Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeJS / Express #16

Open
Seasons123 opened this issue Apr 20, 2017 · 2 comments
Open

NodeJS / Express #16

Seasons123 opened this issue Apr 20, 2017 · 2 comments

Comments

@Seasons123
Copy link
Owner

Seasons123 commented Apr 20, 2017

一、
【1 what is “app.use”?】
The app object is instantiated on creation of the Express server. It has a middleware stack that can be customized in app.configure()(this is now deprecated in version 4.x).

To setup your middleware, you can invoke app.use(<specific_middleware_layer_here>) for every middleware layer that you want to add (it can be generic to all paths, or triggered only on specific path(s) your server handles), and it will add onto your Express middleware stack. Middleware layers can be added one by one in multiple invocations of use, or even all at once in series with one invocation. See use documentation for more details.

To give an example for conceptual understanding of Express Middleware, here is what my app middleware stack (app.stack) looks like when logging my app object to the console as JSON:

stack: 
   [ { route: '', handle: [Function] },
     { route: '', handle: [Function: static] },
     { route: '', handle: [Function: bodyParser] },
     { route: '', handle: [Function: cookieParser] },
     { route: '', handle: [Function: session] },
     { route: '', handle: [Function: methodOverride] },
     { route: '', handle: [Function] },
     { route: '', handle: [Function] } ]

As you might be able to deduce, I called app.use(express.bodyParser()), app.use(express.cookieParser()), etc, which added these express middleware 'layers' to the middleware stack. Notice that the routes are blank, meaning that when I added those middleware layers I specified that they be triggered on any route. If I added a custom middleware layer that only triggered on the path /user/:id that would be reflected as a string in the route field of that middleware layer object in the stack printout above.

Each layer is essentially adding a function that specifically handles something to your flow through the middleware.

E.g. by adding bodyParser, you're ensuring your server handles incoming requests through the express middleware. So, now parsing the body of incoming requests is part of the procedure that your middleware takes when handling incoming requests -- all because you called app.use(bodyParser).

http://stackoverflow.com/questions/11321635/nodejs-express-what-is-app-use

【2 响应方法】
下表中响应对象 (res) 的方法可以向客户机发送响应,并终止请求/响应循环。如果没有从路由处理程序调用其中任何方法,客户机请求将保持挂起状态。

方法	描述
res.download()	提示将要下载文件。
res.end()	结束响应进程。
res.json()	发送 JSON 响应。
res.jsonp()	在 JSONP 的支持下发送 JSON 响应。
res.redirect()	重定向请求。
res.render()	呈现视图模板。
res.send()	发送各种类型的响应。
res.sendFile	以八位元流形式发送文件。
res.sendStatus()	设置响应状态码并以响应主体形式发送其字符串表示。

【3 Difference between app.use and app.get in express.js】
app.use() is intended for binding middleware to your application. The path is a "mount" or "prefix" path and limits the middleware to only apply to any paths requested that begin with it. It can even be used to embed another application:

// subapp.js
var express = require('express');
var app = modules.exports = express();
// ...
// server.js
var express = require('express');
var app = express();

app.use('/subapp', require('./subapp'));

// ...

By specifying / as a "mount" path, app.use() will respond to any path that starts with /, which are all of them and regardless of HTTP verb used:

GET /
PUT /foo
POST /foo/bar
etc.

app.get(), on the other hand, is part of Express' application routing and is intended for matching and handling a specific route when requested with the GET HTTP verb:
GET /
And, the equivalent routing for your example of app.use() would actually be:

app.all(/^\/.*/, function (req, res) {
    res.send('Hello');
});
@Seasons123 Seasons123 changed the title NodeJS / Express: what is “app.use”? NodeJS / Express Apr 20, 2017
@Seasons123
Copy link
Owner Author

Seasons123 commented Apr 23, 2017

@Seasons123
Copy link
Owner Author

Seasons123 commented Jan 8, 2019

三、node版本管理工具——Nodist

1.完全卸载Node.js
首先卸载Node.js应用程序 确认在C:\Program Files中没有Nodejs目录 确认在C:\Program Files (x86)没
有Nodejs目录 删除C:\Users{User}\AppData\Roaming\npm这个目录 删除C:\Users{User}\AppData\Roaming\npm-cache这个目录 确认在环境变量中没有对Node.js和npm的引

2.安装Nodist
项目地址:https://github.com/marcelklehr/nodist 下载installer 安装

3.查看Nodist所有版本
nodist

4.下载Nodist最新版本
nodist latest

5.安装某个版本的Nodist
nodist 4.4.5(前提是还没有安装该版本) 确认与Nodist的4.4.5版本对应Node版本:node -v(node的版
本和nodist的版本是对应的)

6.查看Node版本
node -v

7.更换淘宝镜像,加速
进入Nodist安装目录,打开cli.js,将distUrl、iojsDistUrl更换成如下即可:
var distUrl = 'https://npm.taobao.org/mirrors/node';
var iojsDistUrl = 'https://npm.taobao.org/mirrors/iojs';

8.npm加速
npm config set registry https://registry.npm.taobao.org

9.常用命令

nodist //查看当前安装了的node版本列表
nodist 4.4.6 //设置全局node的版本,如果该版本不存在,会先下载node
nodist + 4.4.6 //检查该版本有没有安装,如果没有,会进行下载
nodist - 1.1.3 //移除某个版本
nodist 4.4.6  //切换到某个版本的Nodist(前提是已安装该版本)

在Windows下使用Nodist进行Node版本控制
Windows下使用Nodist管理多个node版本

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant