What is Express.js? [closed]
Asked Answered
B

10

505

I am a learner in Node.js.

  1. What's Express.js?
  2. What's the purpose of it with Node.js?
  3. Why do we actually need Express.js? How is it useful for us to use with Node.js?
  4. What's Redis? Does it come with Express.js?
Barnaul answered 27/9, 2012 at 7:24 Comment(0)
S
300

This is over simplifying it, but Express.js is to Node.js what Ruby on Rails or Sinatra is to Ruby.

Express 3.x is a light-weight web application framework to help organize your web application into an MVC architecture on the server side. You can use a variety of choices for your templating language (like EJS, Jade, and Dust.js).

You can then use a database like MongoDB with Mongoose (for modeling) to provide a backend for your Node.js application. Express.js basically helps you manage everything, from routes, to handling requests and views.

Redis is a key/value store -- commonly used for sessions and caching in Node.js applications. You can do a lot more with it, but that's what I'm using it for. I use MongoDB for more complex relationships, like line-item <-> order <-> user relationships. There are modules (most notably connect-redis) that will work with Express.js. You will need to install the Redis database on your server.

Here is a link to the Express 3.x guide: https://expressjs.com/en/3x/api.html

Sinistrodextral answered 27/9, 2012 at 7:27 Comment(6)
Express is more like sinatra rather than rails.Ulane
Express was specifically inspired by sinatra. Probably its most important feature is routing; it also makes available all the middleware provided by connect (to the point where all but the most specicialized applications that need connect's middleware will likely use express).Ceric
I would say redis is ideal for complex relationships in fact.Toodleoo
The EJS link is currently dead.Aggie
I really do not understand how MVC relates to Express. If I recall correctly Express has no models and no views. It's a middleware-based framework, not MVC-framework. Please explain this.Hartill
"This is over simplifying it, but Express.js is to Node.js what Ruby on Rails or Sinatra is to Ruby." this means nothing to me, who don't know a thing about Ruby.Lavatory
C
439

1) What is Express.js?

Express.js is a Node.js framework. It's the most popular framework as of now (the most starred on NPM).

Enter image description here.

It's built around configuration and granular simplicity of Connect middleware. Some people compare Express.js to Ruby Sinatra vs. the bulky and opinionated Ruby on Rails.

2) What is the purpose of it with Node.js?

That you don't have to repeat same code over and over again. Node.js is a low-level I/O mechanism which has an HTTP module. If you just use an HTTP module, a lot of work like parsing the payload, cookies, storing sessions (in memory or in Redis), selecting the right route pattern based on regular expressions will have to be re-implemented. With Express.js, it is just there for you to use.

3) Why do we actually need Express.js? How it is useful for us to use with Node.js?

The first answer should answer your question. If no, then try to write a small REST API server in plain Node.js (that is, using only core modules) and then in Express.js. The latter will take you 5-10x less time and lines of code.

What is Redis? Does it come with Express.js?

Redis is a fast persistent key-value storage. You can optionally use it for storing sessions with Express.js, but you don't need to. By default, Express.js has memory storage for sessions. Redis also can be use for queueing jobs, for example, email jobs.

Check out my tutorial on REST API server with Express.js.

MVC but not by itself

Express.js is not an model-view-controller framework by itself. You need to bring your own object-relational mapping libraries such as Mongoose for MongoDB, Sequelize (http://sequelizejs.com) for SQL databases, Waterline (https://github.com/balderdashy/waterline) for many databases into the stack.

Alternatives

Other Node.js frameworks to consider (https://www.quora.com/Node-js/Which-Node-js-framework-is-best-for-building-a-RESTful-API):

UPDATE: I put together this resource that aid people in choosing Node.js frameworks: http://nodeframework.com

UPDATE2: We added some GitHub stats to nodeframework.com so now you can compare the level of social proof (GitHub stars) for 30+ frameworks on one page.

enter image description here

Full-stack:

Just REST API:

Ruby on Rails like:

Sinatra like:

Other:

Middleware:

Static site generators:

Crinkle answered 27/9, 2012 at 7:24 Comment(4)
Will it be correct to say that - Express.js is a Node.js module (which you have addressed as framework in your 1st line). I've heard the term nodeJs module quite often. Framework inside a framework is bit news to me.Allenaallenby
Yes, as in practice, Express IS a Node Package Manager (npm) module (a.k.a. a package) in as much as it's added to an application like other modules from npm. (If you look at the source folder of a Node Express application, you'll see a folder called node_modules. This will contain a sub-folder called express.) But it's far more useful than most other modules off the npm because it has a load of dependent modules that effectively provide the app coder with an API for common tasks. Framework inside a framework ? More like an implementation framework attachable to a server-creation framework.Trost
I don't feel you're explaining what it does at all, you should explain briefly how the web works how developers used to work and how it evolved so anyone will see clearly what did express in order to improve thingsMarcomarconi
@Allenaallenby did you refer to nodejs as the outer framework by the phrase "framework inside a framework"? Also, I always thought of express as a library than a framework.Chesser
S
300

This is over simplifying it, but Express.js is to Node.js what Ruby on Rails or Sinatra is to Ruby.

Express 3.x is a light-weight web application framework to help organize your web application into an MVC architecture on the server side. You can use a variety of choices for your templating language (like EJS, Jade, and Dust.js).

You can then use a database like MongoDB with Mongoose (for modeling) to provide a backend for your Node.js application. Express.js basically helps you manage everything, from routes, to handling requests and views.

Redis is a key/value store -- commonly used for sessions and caching in Node.js applications. You can do a lot more with it, but that's what I'm using it for. I use MongoDB for more complex relationships, like line-item <-> order <-> user relationships. There are modules (most notably connect-redis) that will work with Express.js. You will need to install the Redis database on your server.

Here is a link to the Express 3.x guide: https://expressjs.com/en/3x/api.html

Sinistrodextral answered 27/9, 2012 at 7:27 Comment(6)
Express is more like sinatra rather than rails.Ulane
Express was specifically inspired by sinatra. Probably its most important feature is routing; it also makes available all the middleware provided by connect (to the point where all but the most specicialized applications that need connect's middleware will likely use express).Ceric
I would say redis is ideal for complex relationships in fact.Toodleoo
The EJS link is currently dead.Aggie
I really do not understand how MVC relates to Express. If I recall correctly Express has no models and no views. It's a middleware-based framework, not MVC-framework. Please explain this.Hartill
"This is over simplifying it, but Express.js is to Node.js what Ruby on Rails or Sinatra is to Ruby." this means nothing to me, who don't know a thing about Ruby.Lavatory
R
21
  1. What is Express.js?

Express.js is a Node.js web application server framework, designed for building single-page, multi-page, and hybrid web applications. It is the de facto standard server framework for node.js.

Frameworks built on Express.

Several popular Node.js frameworks are built on Express:

LoopBack: Highly-extensible, open-source Node.js framework for quickly creating dynamic end-to-end REST APIs.

Sails: MVC framework for Node.js for building practical, production-ready apps.

Kraken: Secure and scalable layer that extends Express by providing structure and convention.

MEAN: Opinionated fullstack JavaScript framework that simplifies and accelerates web application development.

  1. What is the purpose of it with Node.js?
  2. Why do we actually need Express.js? How it is useful for us to use with Node.js?

Express adds dead simple routing and support for Connect middleware, allowing many extensions and useful features.

For example,

  • Want sessions? It's there
  • Want POST body / query string parsing? It's there
  • Want easy templating through jade, mustache, ejs, etc? It's there
  • Want graceful error handling that won't cause the entire server to crash?
Rosenwald answered 12/4, 2016 at 11:44 Comment(0)
V
13
  1. Express.js is a modular web framework for Node.js
  2. It is used for easier creation of web applications and services
  3. Express.js simplifies development and makes it easier to write secure, modular and fast applications. You can do all that in plain old Node.js, but some bugs can (and will) surface, including security concerns (eg. not escaping a string properly)
  4. Redis is an in-memory database system known for its fast performance. No, but you can use it with Express.js using a redis client

I couldn't be more concise than this. For all your other needs and information, Google is your friend.

Volatilize answered 30/4, 2015 at 21:46 Comment(0)
F
8

ExpressJS is bare-bones web application framework on top of NodeJS.

It can be used to build WebApps, RESTFUL APIs etc quickly.

Supports multiple template engines like Jade, EJS.

ExpressJS keeps only a minimalist functionality as core features and as such there are no ORMs or DBs supported as default. But with a little effort expressjs apps can be integrated with different databases.

For a getting started guide on creating ExpressJS apps, look into the following link:

ExpressJS Introductory Tutorial

Fourdrinier answered 29/8, 2016 at 21:37 Comment(0)
W
4

Express is a module framework for Node that you can use for applications that are based on server/s that will "listen" for any input/connection requests from clients. When you use it in Node, it is just saying that you are requesting the use of the built-in Express file from your Node modules.

Express is the "backbone" of a lot of Web Apps that have their back end in NodeJS. From what I know, its primary asset being the providence of a routing system that handles the services of "interaction" between 2 hosts. There are plenty of alternatives for it, such as Sails.

Wizardry answered 15/2, 2017 at 9:29 Comment(0)
F
2

Express.js is a framework used for Node and it is most commonly used as a web application for node js.

Here is a link to a video on how to quickly set up a node app with express https://www.youtube.com/watch?v=QEcuSSnqvck

Foofaraw answered 11/9, 2016 at 3:57 Comment(0)
Z
2

Express.js created by TJ Holowaychuk and now managed by the community. It is one of the most popular frameworks in the node.js. Express can also be used to develop various products such as web applications or RESTful API.For more information please read on the expressjs.com official site.

Zacatecas answered 22/5, 2017 at 5:52 Comment(0)
S
1

ExpressJS is a web application framework that provides you with a simple API to build websites, web apps and back ends. With ExpressJS, you need not worry about low level protocols, processes, etc. Fast, unopinionated, minimalist web framework for Node.js

Pug (earlier known as Jade) is a terse language for writing HTML templates. It −

Produces HTML Supports dynamic code Supports reusability (DRY) It is one of the most popular template language used with Express.

Singlehearted answered 20/4, 2021 at 18:28 Comment(0)
A
1

A perfect example of its power


router.route('/recordScore').post(async(req, res) => {   



  
  let gold_nation = req.body.gold && req.body.gold.nationality;
  let silver_nation = req.body.silver && req.body.silver.nationality;
  let bronze_nation = req.body.bronze && req.body.bronze.nationality;
  let competition_id = req.body.competition_id;

  console.log(gold_nation)
  console.log(silver_nation)
  req.body.gold && await country.updateOne({"flag" : gold_nation}, { $inc: { gold: 1 } });
  
  req.body.silver && await country.updateOne({"flag" : silver_nation}, { $inc: { silver: 1 } });
 
  req.body.bronze && await country.updateOne({"flag" : bronze_nation}, { $inc: { bronze: 1 } });
  console.log(competition_id)
  //await competition.updateOne({"_id" : competition_id}, {$set: {recorded : true}});
//!! Uncomment this and change model/competition.ts set recorer to recorded
// this is commented out so you can test adding medals for every case and not creating competitions every time
  res.status(200).json("Success");

});



  async record(){
    let index = this.competitions.findIndex(e => e._id == this.selectedCompetition);
    let goldIndex = this.competitors.findIndex(e => e._id == this.goldWinner);
    let silverIndex = this.competitors.findIndex(e => e._id == this.silverWinner);
    let bronzeIndex = this.competitors.findIndex(e => e._id == this.bronzeWinner);

    console.log(this.competitors[goldIndex]);
    console.log(this.competitors[1-goldIndex]);

    this.sportService.recordCompetition(this.competitors[goldIndex], 
                                    this.competitors[1-goldIndex],
                                    null,
                                    this.competitions[index]).subscribe((m:string) => this.reset(m))



    }

    reset(message: string){
      this.statusMessage = message;
        if(message == "Success"){
          
          this.competitions = this.competitions.filter( (c) => c._id != this.selectedCompetition);
          this.goldWinner = '';
          this.silverWinner = '';
          this.bronzeWinner = '';
        }
  
  
        setTimeout(()=>{                          
          this.statusMessage = '';
        }, 3000);
    
    }


router.route('/registerCompetitor').post(async(req, res) => {   


  //! PROVJERI DA LI JE FORMIRANJE TAKMICENJA ZAVRSENO

  let competitors = req.body.map( c => ({
    name: c.name,
    gender: c.gender,
    nationality: c.nationality,
    sport: c.sport,
    disciplines: c.disciplines
  }));
  console.log(competitors)
  await country.updateOne({"flag" : competitors[0].nationality}, { $inc: { numberOfCompetitors: competitors.length } });


  await   competitor.collection
                                    .insertMany(competitors)
                                    .then( u => {
                                      res.status(200).json("Ok")
                                    })
                                    .catch(err =>{ res.status(400).json("notOk");
                                  });
  
  
});


Asphalt answered 29/9, 2021 at 14:58 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Apostatize

© 2022 - 2024 — McMap. All rights reserved.