This tutorial is prepared to make you familiar with Express.js. Express is a web framework for node.js that allows you to build a minimal and flexible web application. We will go through some basic concepts of Express in this tutorial and demonstrate various topics of express.js such as routing, middleware, route chaining and more.
I assume you have the knowledge of following topics to follow along with this tutorial.
Postman is a popular API end point testing application
Upon completion of this tutorial, you will learn:
"Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications" - Express
As a full-stack javascript developer, I use Express to make a web server and API in the node.js runtime environment. You can simply install the express as a node package in your application. The complexity of creating a server and handling different routes by using the core http module is reduced by the express package.
For example the code for creating http server using http core module looks like:
var http = require('http');
//create a server object:
http
.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
})
.listen(8080); //the server object listens on port 8080
In the above code, we have imported the http core module from node and created the server that listens on port 8080. By using express we can acheive same thing simply as:
//this is es6 syntax for importing packages
import express from 'express';
const app = express();
app.listen(8080, () => {
// on the server
console.log(`Your Server is running on port 8080`);
});
There are lot of cool features offered by express.js through different methods. You will get indepth understanding of Express while you follow along this tutorial. So lets get started!
As Express is node package, first we need to install node and npm in our machine. NPM comes along with the installation of Nodejs. You can download and install NodeJS from official site.
After you installed NodeJS and NPM in your machine, simply run the following commands to verify if its installed properly. You will get a version number in return
node -v
npm -v
mkdir express-app && cd express-app
npm init
npm install express
npm install nodemon
nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected..
Since we are going to use ES6 syntax, we need a babel to transform our ES6 code into a code that browser can interpret without error. To install babel and successfully use it on our project we need to install couple of babel packages.
npm install --save-dev babel-cli babel-preset-env babel-preset-stage-0
Now its time to configure nodemon and babel in our project. Instead of running npm index.js
, we will configure nodemon in our package.json file. Lets change the "start" scripts in package.json file to this:
"scripts": {
"start": "nodemon ./index.js --exec babel-node -e js"
}
Hi, I am Ajeet, a pragmatic software engineer based in Melbourne, Australia. I write an article about modern software development and my side projects. If this site has helped you somehow to learn, I would be grateful if you consider supporting me.
Buy me a CoffeeSignup my newsletter to get update when any new stuffs comes out!
Comments