- Initialized
npm
project withpackage.json
file. See: Getting started withnpm
. express
installed as project dependency.- Entry page (i.e.
server.js
) present in the project root.
project-root
├── node_modules
├── package-lock.json
├── package.json
└── server.js
-
In your favourite editor (VSCode), open the file
server.js
-
Add JS code to load express as a program dependency:
const express = require('express');
-
Create an instance of the Express server. Express wants you to start by invoking the function that
express
exports torequire()
.const app = express();
express()
returns a JS object. It includes many functions that come in handy. Let's use those functions now.
-
Set a variable for the port that the server will listed to. For now, let's go with port 4000:
const PORT = 4000;
-
Instruct the server to listen to the PORT:
app.listen(PORT, function(){ console.log(`Listening on port ${PORT}`); });
- Our node program (an Express server!) will start in the terminal window (making it pretty useless, besides logging to the console).