Basic express.js Development Server Setup

To set up a server using express.js, add the following code to src/index.js:

// index.js
// This is the main entry point of our application

const express = require('express');
const app = express();
const port = process.env.PORT || 4000;

app.get('/', (req, res) => res.send('Hello World!!!'));

app.listen(port, () =>
  console.log(`Server running at http://localhost:${port}`)
);

Start the server, and listen on port 4000, with this command:

node src/index.js

You can now browse to http://localhost:4000, to see Hello World!!!

Any change to the code, will require a restart to the server, and a browser refresh.

To automate this, add the following to your package.json:

"dev": "nodemon src/index.js",

You can now use node to monitor for any changes, with this command:

npm run dev

Leave a Reply

Your email address will not be published. Required fields are marked *