I know, that was a long title. Anyway—today I’ll show you how to get started with back-end development using Node.js and Express. The plan is to create a simple web server that can handle requests and send responses. This is the start of a series on getting started with Node.js.
Originally published on Medium in FAUN.dev().
I hope you know the terminology of the web and have a good idea of how it works—otherwise most of this article won’t land. If you need a refresher, read this first, then come back to build your first web server in JavaScript:
Previous article: What Happens When You Visit a Webpage?
Environment setup
You don’t need anything fancy, but there are a few steps. First, install Node.js—the latest LTS—from the official Node.js site. Search for “Node JS download,” open the first result, and grab the LTS build. During install, make sure “Add to PATH” is checked (it’s usually on by default); otherwise keep clicking Next and finish the install.
Create a project folder—mine was A:\projects\first_node_server. Open that folder in your editor. I’m using VS Code; you can open the terminal with Ctrl+`.

Now as you’ve installed Node, you also have npm (Node Package Manager). To initialize a Node project, run:
npm init
The npm CLI will ask for project name, author, description, license, and so on. Change the values if you want, or keep hitting Enter—it doesn’t matter much at this stage.
That creates a package.json in your project directory with name, author, description, scripts, and the list of installed packages (empty for now, since nothing is installed yet).
These packages are external libraries—other people’s code you want to use in your project.
Express is not a native Node module, so install it with:
npm install express --save
npm installs Express, creates a node_modules folder for packages installed into the project, and generates package-lock.json. That lockfile is created whenever npm changes node_modules or package.json. It describes the exact dependency tree so later installs can reproduce the same tree, even if intermediate dependencies change.
That’s a dense definition—if it doesn’t click yet, you can ignore it for now.
Initializing Express and starting an HTTP server
Create a file named app.js. The name is a convention—you can call it something else, but conventions make it easier for others to follow your code. We usually split concerns into app.js (initialize the app) and server.js (start and manage the server). For this small example, we’ll do both in app.js.

In the image above, you can see the code—I’ll explain it line by line. Here’s the same file for easy copying:
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.end('Hi there, Server is working.')
})
app.listen(9000, '127.0.0.1', () => {
console.log('Server is running on port 9000')
})
Line by line:
- The first line imports the Express library we just installed. You might wonder why not use an ES
importstatement. You can try, but Node will throwCannot use import statement outside a modulebecause Node doesn’t enable ES modules by default. There’s experimental support—you need changes inpackage.jsonto turn it on. More detail here: How to use an ES6 import in Node.js? — GeeksforGeeks. - Next we call the Express module; it returns an app object we store in
app. app.gethandles GET requests to the path/. The first argument is the path; the second is a callback that runs when a matching request arrives:
app.[http_method]([path_of_the_incoming_request], (req, res) => {
// Runs when a request hits the specified path
})
/ is the domain root / home page—e.g. www.google.com is the same as www.google.com/. Here we’re listening for GET requests on the base path and responding with the string Hi there, Server is working.
The callback’s first parameter is the request object (req); the second is the response object (res).
The request object has details about the incoming request—protocol, hostname, body, params, and more.
We use the response object to send data back. res.end() ends the response process. You can pass what you want to send to the client:
res.end('Hi there, Server is working.')
Syntax: res.end([data] [, encoding])
- Finally we start the server. Express provides
listenon the app object:
app.listen(9000, '127.0.0.1', () => {
console.log('Server is running on port 9000')
})
The first argument is the port (required). The second is the hostname (optional)—127.0.0.1 is localhost; if you omit it, localhost is used anyway. The last argument is a callback that runs once the server has started; we log that it’s running on port 9000.
Run the script with:
node app.js
Then open your browser to 127.0.0.1:9000.

Remember to run npm init and install Express before running this file, or you’ll get an error for obvious reasons.
That’s it—that’s how you initialize a simple web server with Node and Express. Next up: send an HTML file as the response and serve static assets—then later, reading and writing files with Node.js.
Next article: Sending an HTML File in Response and Serving Static Files with Express
Thanks for reading—see you next time.



