What is Express?


Express is a small and light Node.js web application framework that provides good set of features for web and mobile application development. Building an Application Programming Interface (API).

Why Express, you may ask. - It's fairly easy and has a small learning curve. It's open-source and also maintained by the NodeJS Foundation. Unlike Rails and Django which have an opinionated way, Express has no "best-way" to reach your goal. It's flexible and can be use to build almost any thing.


How do I Install it?


Assuming you have Node Package Manager (npm) installed, Installing Express is easy as running a line of code. 

In your Node application, navigate to the root directory,  Open your terminal/cmd and run this command -

npm install express --save

Notice the --save flag we used there? - This ensures that express is added as a dependency in your project's package.json file. So next time you run it on a different workstation or someone downloads your code, they can run npm install to install all the dependencies required to run your project. - Neat! 


How do I Use it?


In your index/main.js file, require express and initialize express with a variable (here is app). To listen to a GET Request, use the following code - 

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

app.get('/', function(req, res){
   res.send("Hello world!");


In the above code snippet we import express using the require keyword initialize express with the variable 'app'. Using the variablle we listen to GET request at route '/'. The get callback has two parameters request(req) and response(res). The request object(req) represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, etc. Similarly, the response object represents the HTTP response that the Express app sends when it receives an HTTP request, basically contains the data we requested for.

res.send("Hello World!") - This function takes an object as input and sends this to the requesting client. Here we're sending the string "Hello World!".

app.listen(3000) This function binds and listens for connections on the specified port. We can also specify the host as a parameter in the function


Listening to Requests at different Routes


Web frameworks provide resources such as HTML pages, scripts, images, etc. at different routes. We use express built in routing feature to define routes for various functions. The syntax is like -

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

app.get('/hello', function(req, res){
   res.send("Hello World!");

app.get('/hi', function(req, res){
   res.send("Hi World!");

app.get('/api', function(req, res){
   res.send("You have reached localhost:3000/api");


Here, we listen to a GET request at path '/hello' , '/hi' , '/api' . When we run the application and navigate to "localhost:3000/hello", our express app executes the callback function attached to this route, in this case it sends a "Hello World!" response to the client. Similarly for "localhost:3000/hi" and "localhost:3000/api" we get the responses "Hi World!" and "You have reached localhost:3000/api" as strings.

To test the routes, open your terminal and send a cURL request or download a chrome extension called 'Postman' and send requests on these routes, check for the output. We can define as many routes as we want, take in parameters, queries and use them to compute or fetch certain record from a database. Benefit of using an API is we can design multi-platform apps and centralize our data without any fuss.

But defining routes like above is very tedious to maintain. In the next post we will learn to separate the routes from our main index.js file, by using Express.Router. 



Getting Started - https://www.thecodingpup.com/post_detailed/getting-started-with-nodejs

Express.Router() - https://www.thecodingpup.com/post_detailed/maintain-nodejs-api-endpoints-using-expressrouter

blog comments powered by Disqus