node.js Express.js 在 MEAN 堆栈中做了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19411135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does Express.js do in the MEAN stack?
提问by NicolasMoise
I have recently have gotten into AngularJS and I love it. For an upcoming project I am looking to use the MEAN stack (MongoDB, Express, Angular, Node). I'm pretty familiar with Angular and I have a modest understanding of the purposes of MongoDB and Node in the stack. However, I don't really understand what the purpose of Express.js is. Is it essential to the MEAN stack? What would you compare it to in a traditional MySQL, PHP, javascript app? What does it do that the other three components can't do?
我最近开始接触 AngularJS,我喜欢它。对于即将到来的项目,我希望使用 MEAN 堆栈(MongoDB、Express、Angular、Node)。我对 Angular 非常熟悉,并且对 MongoDB 和 Node 在堆栈中的用途有一定的了解。但是,我真的不明白 Express.js 的目的是什么。它对 MEAN 堆栈必不可少吗?您会将它与传统的 MySQL、PHP、javascript 应用程序进行比较吗?它有什么其他三个组件不能做的?
Also, if someone wants to give their own take on how the four parts of the stack work together, that'd be great.
此外,如果有人想对堆栈的四个部分如何协同工作发表自己的看法,那就太好了。
回答by jakee
- MongoDB = database
- Express.js = back-end web framework
- Angular = front-end framework
- Node = back-end platform / web framework
- MongoDB = 数据库
- Express.js = 后端 Web 框架
- Angular = 前端框架
- Node=后端平台/Web框架
Basically, what Express does is that it enables you to easily create web applications by providing a slightly simpler interface for creating your request endpoints, handling cookies, etc. than vanilla Node. You could drop it out of the equation, but then you'd have to do a lot more work in whipping up your web-application. Node itself could do everything express is doing (express is implemented with node), but express just wraps it up in a nicer package.
基本上,Express 所做的是通过提供一个比普通 Node.js 更简单的接口来创建请求端点、处理 cookie 等,从而使您能够轻松创建 Web 应用程序。您可以将它排除在等式之外,但随后您必须做更多的工作来启动您的 Web 应用程序。Node 本身可以做 express 所做的一切(express 是用 node 实现的),但 express 只是将它包装在一个更好的包中。
I would compare Express to some PHP web framework in the stack you describe, something like slim.
我会将 Express 与您描述的堆栈中的某些 PHP Web 框架进行比较,例如slim。
回答by Jondlm
You can think of Express as a utility belt for creating web applications with Node.js. It provides functions for pretty much everything you need to do to build a web server. If you were to write the same functionality with vanilla Node.js, you would have to write significantly more code. Here are a couple examples of what Express does:
您可以将 Express 视为使用 Node.js 创建 Web 应用程序的实用工具。它提供了构建 Web 服务器所需的几乎所有功能。如果您要使用 vanilla Node.js 编写相同的功能,您将不得不编写更多的代码。以下是 Express 功能的几个示例:
- REST routes are made simple with things like
app.get('/user/:id', function(req, res){ /* req.params('id') is avail */ });
- A middleware system that allows you plug in different synchronous functions that do different things with a request or response, ie. authentication or adding properties
app.use(function(req,res,next){ req.timestamp = new Date(); next(); });
- Functions for parsing the body of POST requests
- Cross site scripting prevention tools
- Automatic HTTP header handling
app.get('/', function(req,res){ res.json({object: 'something'}); });
- REST 路由通过类似的东西变得简单
app.get('/user/:id', function(req, res){ /* req.params('id') is avail */ });
- 一个中间件系统,允许您插入不同的同步功能,这些功能对请求或响应执行不同的操作,即。身份验证或添加属性
app.use(function(req,res,next){ req.timestamp = new Date(); next(); });
- 用于解析 POST 请求正文的函数
- 跨站脚本预防工具
- 自动 HTTP 标头处理
app.get('/', function(req,res){ res.json({object: 'something'}); });
Generally speaking, Sinatra is to Ruby as Express is to Node.js. I know it's not a PHP example, but I don't know much about PHP frameworks.
一般来说,Sinatra 之于 Ruby,就像 Express 之于 Node.js。我知道这不是 PHP 示例,但我对 PHP 框架了解不多。
回答by jeremy
Express handles things like cookies, parsing the request body, forming the response and handling routes.
Express 处理诸如 cookie、解析请求正文、形成响应和处理路由之类的事情。
It also is the part of the application that listens to a socket to handle incoming requests.
它也是应用程序的一部分,它侦听套接字以处理传入的请求。
A simple example from express github
来自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, creating a route app.get('/'...and opening the port to listen for incoming http requests on.
显示快速服务器的创建,创建路由app.get('/'...并打开端口以侦听传入的 http 请求。
回答by Mile Mijatovi?
Express in a Node.js based framework which simplifies writing Server-side Code and Logic.
在基于 Node.js 的框架中表达,该框架简化了服务器端代码和逻辑的编写。
Adds a lot of utility features and offers additional functionality, and in general, makes things easier.
添加了许多实用功能并提供了额外的功能,总的来说,让事情变得更容易。
Express is middleware-based: It basically funnels incoming requests through a chain of middlewares (of 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 back a response immediately.
Express 是基于中间件的:它基本上通过一系列中间件(步骤)汇集传入请求,我们可以在其中对请求执行某些操作,从中读取一些数据,对其进行操作,检查用户是否已通过身份验证或基本上发回响应立即地。
This middlewares chain allows us to write very structured code
这个中间件链允许我们编写非常结构化的代码
回答by Y Munawwer
Express is a nodejs Framework build upon the top of Http modulewith more usable and better functionalities like easy way to handle routes.
Express 是一个构建在 Http 模块之上的 nodejs 框架,具有更多可用和更好的功能,例如处理路由的简单方法。
eg: Using HTTP
例如:使用 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();
})
回答by Sai anurag
Express allows you to manage http request easily compared to vanilla js. you need to the following to make a get request
与普通 js 相比,Express 允许您轻松管理 http 请求。您需要执行以下操作才能发出获取请求
const Http = new XMLHttpRequest();
const url='https://jsonplaceholder.typicode.com/posts';
Http.open("GET", url);
Http.send();
Http.onreadystatechange=(e)=>{
console.log(Http.responseText)
}
In express, you require express and use it and make http requests
在 express 中,您需要 express 并使用它并发出 http 请求
const express = require("express")
const app =express();
app.get("url",callback function);

