Step 1

Creating a server

Initialize the new Node JS project.

$ npm init -y
$ npm install -S express

The first command will create a "package.json" file. This file gives NodeJS all the information it needs to know about your project, including what dependencies it needs.

The second command installs a package called "express", which is a very popular framework for creating web servers.

Let's create a Web API

Create a folder called "src", and inside that, create a file called "index.js". In this file, put the following:

const express = require('express');

const PORT = process.env.PORT || 5000;
const app = express();

app.listen(PORT, () => {
	console.log(`Listening on port: ${PORT}`);
});

And that's it! With just 8 lines of code, you have created an HTTP server that listens for requests on port 5000.

Let's run our app! Open "package.json" and put the following in the "scripts" section:

...
    "main": "src/index.js",
	"scripts": {
		"start": "node src/index.js",
		"test": "echo \"Error: no test specified\" && exit 1"
	},
	"repository": {
....

It doesn't do anything important right now, but that will come in step 2.

Last updated

Was this helpful?