Node.js express quick start


We require the following to be setup, on your computer:

  • Node.js (As of this date, I have 6.11.0)
  • Any text editor (I prefer to use Sublimetext or Microsoft's Visual Studio Code)

First, create a folder called "my-website" on Desktop. Open a terminal / command prompt and change to the newly created directory. A node.js project usually has a project management file called package.json. To create a default package.json configuration file, issue the following command:

npm init

This will ask a series of questions (mostly with default answers). Answer all the questions with relevant answers, and confirm the creation of the file. Here is what I have got:

{
  "name": "my-website",
  "version": "1.0.0",
  "description": "Quick starting with node.js and express",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/kayartaya-vinod"
  },
  "keywords": [
    "node",
    "express",
    "website",
    "quickstart",
    "vinod"
  ],
  "author": "Vinod <vinod@vinod.co>",
  "license": "MIT"
}

If you don't want to go through the trouble of entering each answers, you may simply use the -y flag, which creates a simple default package.json file.

npm init -y

Express is a third party package available from http://npmjs.com. You can install this locally for the current project by executing the following command:

npm install --save express

The --save or -S option will update the current package.json file with the downloaded dependency information. After the successful completion of the command, our package.json looks like this:

{
  "name": "my-website",
  "version": "1.0.0",
  "description": "Quick starting with node.js and express",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/kayartaya-vinod"
  },
  "keywords": [
    "node",
    "express",
    "website",
    "quickstart",
    "vinod"
  ],
  "author": "Vinod <vinod@vinod.co>",
  "license": "MIT",
  "dependencies": {
    "express": "^4.15.3"
  }
}

Let's create a folder called www (you may use any other name) under the my-website, from where we will serve static content like HTML, JS or CSS (and other related) files.

Let's add some basic content to our homepage (www/index.html):

<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
</head>
<body>
    <h1>My Website</h1>
    <hr>
    <p>Node.js + Express demo by Vinod.</p>
</body>
</html>

Finally, let's code the server.js (name can be changed as you wish):

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

app.use(express.static("www"));

app.listen(3000, ()=>{
    console.log("...server started at port 3000");
});

console.log("Starting the server...."); 

Here is the complete folder structure and placement of files:

Node.js express quick start

To run the server.js, simply type the command npm start, which will execute the file mentioned in the package.json with "start" attribute. Here is the output of execution:

Vinods-MacBook-Pro:my-website vinodkumar$ npm start

> my-website@1.0.0 start /Users/vinodkumar/Desktop/my-website
> node server.js

Starting the server....
...server started at port 3000

Use any web browser to visit http://localhost:3000/ and you should see something like this:

Node.js express quick start

That's all folks!

Vinod.


Courses

Blog posts

POLICIES

CONTACT