How to deploy an Express app
Deploy a Node.js Express app on Schokr: listen on 0.0.0.0 and your allocated port, upload your code, set the start command and go live.
Updated · 1 min read
To deploy an Express app on Schokr, create a Node.js server, upload your code and package.json, make Express listen on 0.0.0.0 and your allocated port, and set your entry file in the Startup settings.
1. Prepare your app
A minimal production-ready index.js:
const express = require('express')
const app = express()
app.use(express.json())
app.get('/', (req, res) => res.json({ status: 'ok' }))
const port = Number(process.env.APP_PORT || 3000)
app.listen(port, '0.0.0.0', () => {
console.log(`Express listening on 0.0.0.0:${port}`)
})
And package.json:
{
"name": "my-express-app",
"main": "index.js",
"scripts": { "start": "node index.js" },
"dependencies": { "express": "^4.21.0" }
}
2. Create the server
- In the console, create a server with the Node.js template.
- 256 to 512 MB of memory is plenty for most Express APIs.
- Open the Network section and note the port.
3. Upload and configure
- Upload
index.js,package.json,package-lock.jsonand your other source files with the file manager. Skipnode_modules. - Set the port: either put it in a
.envfile asAPP_PORT=<your-port>and load it (see environment variables), or hard-code it. - In your server's Startup settings, set the main file to
index.js(or the start command, depending on what the template exposes).
4. Start and test
Start the server. The console should show npm installing packages, then your "Express listening" line. Open http://<server-address>:<port> to check.
Production tips
- Set
NODE_ENV=productionin your.env. Express then caches view templates and produces less verbose errors. - Serve static files with
express.static:
app.use(express.static('public'))
- If you put a proxy such as Cloudflare in front, tell Express to trust it so
req.ipandreq.protocolare correct:
app.set('trust proxy', 1)
- Add an error handler so one bad request does not crash the process:
app.use((err, req, res, next) => {
console.error(err)
res.status(500).json({ error: 'Internal error' })
})
- Handle unexpected crashes in async code with
process.on('unhandledRejection', console.error)while you debug.
Data storage
There is no built-in database on Schokr. For a single-server app, SQLite works well. For larger needs, use an external database provider and keep its URL in your .env file.
Troubleshooting
EADDRINUSE: see port errors.Cannot find module 'express': see module not found.- Starts fine but the page will not load: see app not reachable.
- Calling the API from a browser on another site fails: see CORS basics.