How to setup Node/Express development environment

In the tutorial, we will show you how to setup Node/Express development environment that includes installation of Nodejs, NPM package, and ‘Express Application Generator’ optionally.

Installing Node

– Go to: https://nodejs.org/en/
– Download the LTS build that is “Recommended for most users”

download nodejs - LTS version

Double-clicking on the downloaded file and following the installation prompts:

install nodejs

Testing your Nodejs and NPM installation:

Create a ‘helloworld.js’, use ‘http’ module to create a Nodejs server createServer().

Start the server:

start nodejs express - print helloworld message

Using NPM

NPM is the most important tool for working with Node applications to fetch any packages (JavaScript libraries) that an application needs for development, testing, and/or production.

>>> Step to do:

Create application directory

Create package.json file

With NodeJs application, ‘package.json’ file contains all the JavaScript “package” dependencies, including the package’s name, version, description, initial file to execute, production dependencies, development dependencies, versions of Node it can work with, etc.

Use the npm init to create ‘package.json’ file:

-> Check content of ‘package.json’ file:

Install Express by NPM

Use command npm install express

The dependencies of ‘package.json’ will now appear at the end of the ‘package.json’ file with ‘Express’.

Use Express to create NodeJs server

Create an ‘index.js’ file in the root of the ‘hellonodejs’ application directory with content below:

app.get() function only responds to HTTP GET requests with the specified URL path (‘/’).

npm express project structure

Start the server

Go to http://127.0.0.1:8000, browser will display ‘Hello World!’.

start nodejs express - print helloworld message

Express Application Generator

We use the ‘Express Application Generator’ tool to generate an Express application skeleton.

Create an Express app named ‘hellonodejs’ with the default settings:

nodejs express project structure

Install all the dependencies for the ‘hellonodejs’ app by NPM:

Run the app:

express app running

See DEBUG command with logging:

Project structure

package.json

– body-parser: parses/extract the body of an incoming HTTP request.
– morgan: An HTTP request logger middleware for node.
– cookie-parser: used to parse the cookie header and populate req.cookies

www file

/bin/www is the application entry point!

/bin/www require() ‘app.js’ file.

app.js

The file creates an express application, sets up the application with various settings and middleware, and then exports the app from the module.

More details with ‘app.js’ file
– Import useful node libraries into the file using require():

require() modules from our routes directory:

– Set up the view (template) engine

– Add the middleware libraries into the request handling chain:

– Add route-handling to the request handling chain:

– Add handler methods for errors and HTTP 404 responses:

– The last step is to add it to the module exports:

Routes

See ‘/routes/index.js’ file:

It loads the express module, and uses it to get an ‘express.Router’ object.

Views

See ‘/views/index.jade’:

The above ‘/routes/index.js’ router will render template ‘/views/index.jade’.
-> Result:

express app running template

By grokonez | April 4, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*