Node.js express quick start

Prerequisites: I assume that you already have downloaded and installed Node.js in your computer, and the path to the %NODE_HOME%\bin has been added to the PATH environment variable.

First, create a folder called “testing-express” under C: drive. Open a command prompt and change to the directory C:\testing-express. To create a default package.json configuration file, use the command “npm init” at the command prompt. The generated package.json would look something like this:

{
	"name": "testing-express",
	"version": "1.0.0",
	"description": "",
	"main": "index.js",
	"scripts": {
		"test": "echo \"Error: no test specified\" && exit 1"
	},
	"author": "Vinod <kayartaya.vinod@gmail.com>",
	"license": "ISC"
}

To add the “express” dependency, type the command “npm install express –save” in the command prompt. The “–save” switch ensures that the version of the “express” module downloaded is updated to the package.json file.

Here is the updated package.json

{
	"name": "testing-express",
	"version": "1.0.0",
	"description": "",
	"main": "index.js",
	"scripts": {
		"test": "echo \"Error: no test specified\" && exit 1"
	},
	"author": "Vinod <kayartaya.vinod@gmail.com>",
	"license": "ISC",
	"dependencies": {
		"express": "^4.12.3"
	}
}

Now create a file called index.js under our work folder, “testing-express”. Code the following in it and save.

var express = require("express");
var app = express();

app.get("/", function(req, resp){
	resp.set("Content-type", "text/plain");
	resp.end("Hello, World!");
});

app.listen(3000);

To run the application, execute the following command at the command prompt:

node index

Finally, open a browser, and visit http://localhost:3000