What is the essence of "Express" in nodejs
发布于 2 年前 作者 safarishi 1188 次浏览 来自 问答

I read some source code of express in github, bug I have a detail that does not understand const express = require(‘express’) const app = express()

app.use((req, res, next) => { // res is what? is the Writable Stream // debug this code I know the res is a Stream, but I can not distinguish the type of Stream is the writeable or readable })

// reference from nodejs api http.createServer((req, res) => { // res is the same as the express’s res // in the doc of nodejs api // res is an http.ServerResponse, which is a Writable Stream })

but when it comes to the express app’s callback -> res Excuse me?

2 回复

@godghdai thanks

I have found the answer from the nodejs api.

Because express callback res in its prototype you can find it is the type of ServerResponse

// the below is the nodejs document Class: http.ServerResponse# Added in: v0.1.17 This object is created internally by an HTTP server–not by the user. It is passed as the second parameter to the ‘request’ event.

The response implements, but does not inherit from, the Writable Stream interface. This is an EventEmitter with the following events:

回到顶部