Run a Node.js or Python web app
Host an Express, Flask or FastAPI app - the two rules it must follow, and how to deploy it.
Choose the Node.js or Python framework when your website needs code running on the server - a login, a database, an API. If it’s only HTML, CSS and JavaScript, a static site is simpler.
The two rules
Your app will only be reachable if it:
- Listens on the port it’s given, which is in the
PORTenvironment variable - not a number you pick yourself. - Listens on
0.0.0.0, notlocalhostor127.0.0.1.
Get either wrong and the app starts happily, but the address won’t load.
Node.js (Express)
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello from Smile Hosting!'));
const port = process.env.PORT || 3000;
app.listen(port, '0.0.0.0', () => console.log(`Listening on ${port}`));
The Start Command is npm start, so add a start script to your package.json:
{
"scripts": { "start": "node index.js" }
}
Or change the Start Command to node index.js in Settings > Build & Output Settings.
Python (Flask)
import os
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello from Smile Hosting!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))
Put flask in a requirements.txt. The Start Command is python app.py.
Python (FastAPI)
Start uvicorn from your code so it picks up the port:
import os
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"message": "Hello from Smile Hosting!"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=int(os.environ["PORT"]))
Put fastapi and uvicorn in requirements.txt, and set the Start Command to python main.py.
Deploy it
- Create a website with the Node.js or Python framework.
- Upload your code in the Source tab, including
package.jsonorrequirements.txt. Don’t upload your dependencies - nonode_modulesorvenvfolders. - Check the Install Command and Start Command under Settings > Build & Output Settings.
- Press Deploy. Your dependencies are installed from
package.jsonorrequirements.txt, then your Start Command runs. - Open the Logs tab and watch it start. When it says it’s listening, open your address.
After changing your code, press Redeploy to restart it with the new version.
WskazówkaPut secrets like database passwords in a
.envfile and read them from your code, rather than writing them into your files. Files starting with a dot are never shown to visitors.