Introduction

If you don't have package.json file in your working directory, create using the following command :

npm init --yes

After that install Express.js:

npm install express --save

This will create node_modules in your current directory and install Express.js and all it's dependencies within that directory.

Start Server

You can start your server using pure Node.js code or you can use Express.js way to create server. Open entry file which is mentioned in package.json file and copy paste the following code:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

First we require the framework and then create an instance of the Express.js. The variable app refers to the Express.js instance. From now on we will use app to specify the Express.js instance.

Then we registered a route using the method app.get(). It takes first parameter as url path and second parameter as the callback handler. When the url path matches, the handler is executed. For any other route, it returns 404 ("Page Not Found").

Finally, we create server using app.listen() method. It takes first parameter as port number, and then a callback method which is executed when server starts.

So let's run the server by running the following command:

node index.js

Now open the browser and access http://localhost:3000, you will see Hello World as the output. 404 Not Found is returned for other url paths:

http://localhost:3000/about

CLI

Use the application generator tool, express-generator, to quickly create an application skeleton. The express-generator package installs the express command-line tool. Use the following command to do so:

npm install express-generator -g

Now you can use express command on your terminal. To see the help options, run:

express -h

The following creates an Express app named myapp. The app will be created in a folder named myapp in the current working directory and the view engine will be set to Pug:

express --view=pug myapp

Here is the output:

 create : myapp
 create : myapp/package.json
 create : myapp/app.js
 create : myapp/public
 create : myapp/public/javascripts
 create : myapp/public/images
 create : myapp/routes
 create : myapp/routes/index.js
 create : myapp/routes/users.js
 create : myapp/public/stylesheets
 create : myapp/public/stylesheets/style.css
 create : myapp/views
 create : myapp/views/index.pug
 create : myapp/views/layout.pug
 create : myapp/views/error.pug
 create : myapp/bin
 create : myapp/bin/www

You can see the application directory structure of a simple express.js framework. Now it is just a framework skeleton. You need to install the framework and few other dependencies to work with Express.js. The package.json file already contains the dependency list. Run npm init to install them:

npm install

Now run the following command to start the server:

DEBUG=myapp:* npm start // For Linux, MacOS
set DEBUG=myapp:* & npm start // For Windows

Now you will most likely to get the following error when you try to access any page:

No default engine was specified and no extension was provided.

If you open routes/index.js file, you will see, we are using res.render() method to render a page. And this method require the view engine to be specified. As the skeleton version didn't provide it we need to do it manually.

To specify the view engine use the following code before the module.exports:

app.set('view engine', 'pug');

Next install pug:

npm install pug --save

Try running the server again and this time you will get another error:

Failed to lookup view "index" in views directory "D:\Playground\expressdemo\views"

The application skeleton didn't come with any view file. So let's create one. Create a file called index.pug within the view folder and paste the following code:

html
	head
		title Express Demo
	body
		h1 Express.js

Now let's start the server and access http://localhost:3000/, you will see the output this time.

The app structure created by the generator is just one of many ways to structure Express apps. Feel free to use this structure or modify it to best suit your needs.