What does Express.js do on the MEAN stack?

I recently got into AngularJS and I like it. For the upcoming project, I want to use the MEAN stack (MongoDB, Express, Angular, Node). I am very familiar with Angular, and I have a modest understanding of the goals of MongoDB and Node on the stack. However, I do not quite understand what the purpose of Express.js is. Is this important for the MEAN stack? What would you compare this to traditional MySQL, PHP, javascript application? What does he do that the other three components cannot do?

Also, if someone wants to give their own idea of ​​how the four parts of the stack work together, it will be great.

+53
angularjs mongodb express
Oct. 16 '13 at 18:35
source share
6 answers
  • MongoDB = database
  • Express .js = web framework
  • Angular = front-end framework
  • Node = base platform / web framework

Basically what Express does, it allows you to easily build web applications, providing a slightly simpler interface for creating request endpoints, processing cookies, etc., than vanilla Node. You can opt out of this equation, but then you will have to do a lot more work whipping the web application. Node itself could do everything that express does (express is implemented using node), but expresses simply wraps it in a more pleasant package.

I would compare Express with some PHP webmap in the stack you are describing, something like slim .

+64
Oct. 16 '13 at 18:41
source share

You can think of Express as a utility belt for building web applications using Node.js. It provides functions for almost everything you need to create a web server. If you wrote the same functionality with vanilla Node.js, you would have to write significantly more code. Here are some examples of what Express does:

  • REST routes are simplified with things like
    • app.get('/user/:id', function(req, res){ /* req.params('id') is avail */ });
  • A middleware system that allows you to connect various synchronous functions that perform different actions with a request or response, i.e. authentication or adding properties
    • app.use(function(req,res,next){ req.timestamp = new Date(); next(); });
  • Functions for parsing the body of POST requests
  • Means of protection against internetworking scenarios.
  • Automatic HTTP Header Processing
    • app.get('/', function(req,res){ res.json({object: 'something'}); });

Generally speaking, Sinatra is Ruby because Express is Node.js. I know this is not an example of PHP, but I know little about the PHP frameworks.

+20
Oct. 16 '13 at 18:59
source share

Express processes things like cookies, analyzes the request body, and forms response and processing routes.

It is also part of an application that listens on a socket to handle incoming requests.

Simple example from express github

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

 app.get ('/', function (req, res) {
   res.send ('Hello World');
 });
 app.listen (3000);

Shows the creation of the express server, the creation of the route app.get('/'... and the opening of the port for listening to incoming HTTP requests.

+4
Oct 16 '13 at 18:41
source share

Express in an environment based on Node.js, which simplifies the writing of server code and logic.

Adds many useful functions and offers additional functionality, and overall simplifies the work.

Express is based on middleware : it mainly routes incoming requests through a chain of middleware (steps) where we can do something with the request, read some data from it, manipulate it, check if the user is authenticated, or basically send a response immediately.

This middleware chain allows us to write very structured code.

+1
Dec 09 '18 at 17:08
source share

Express is a nodejs framework built on top of the Http module with more convenient and enhanced features, such as an easy way to handle routes.

For example: using HTTP

  var http = require('http'); //create a server object: http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); // http header var url = req.url; if(url ==='/about'){ res.write('<h1>about us page<h1>'); //write a response res.end(); //end the response }else if(url ==='/contact'){ res.write('<h1>contact us page<h1>'); //write a response res.end(); //end the response }else{ res.write('<h1>Hello World!<h1>'); //write a response res.end(); //end the response } }).listen(3000, function(){ console.log("server start at port 3000"); //the server object listens on port 3000 }); 

using Express:

 var express = require('express'); var app = express(); app.get('/about',function(req,res)=>{ res.write('<h1>about us page<h1>'); //write a response res.end(); }) 
+1
Apr 13 '19 at 11:27
source share

Express makes it easy to manage http requests compared to vanilla js. you need the following to request a receipt

 const Http = new XMLHttpRequest(); const url='https://jsonplaceholder.typicode.com/posts'; Http.open("GET", url); Http.send(); Http.onreadystatechange=(e)=>{ console.log(Http.responseText) } 

Express requires express, use it and make http requests

 const express = require("express") const app =express(); app.get("url",callback function); 
0
Sep 30 '18 at 11:10
source share



All Articles