本文转载自:众成翻译
译者:网络埋伏纪事
连接:http://www.zcfy.cc/article/1750
原文:https://blog.risingstack.com/your-first-node-js-http-server/html
本章我会指导你启动一个简单的 Node.js HTTP 服务器,并开始为请求服务。node
当开始在 Node.js 中构建基于 HTTP 的应用程序时,内置的 http
/https
模块就是你要与之交互的模块。数据库
如今,咱们开始建立第一个 Node.js HTTP 服务器!咱们将 require http
模块,并将服务器绑定到 3000
端口来监听。express
// index.js 的内容 const http = require('http') const port = 3000 const requestHandler = (request, response) => { console.log(request.url) response.end('Hello Node.js Server!') } const server = http.createServer(requestHandler) server.listen(port, (err) => { if (err) { return console.log('something bad happened', err) } console.log(`server is listening on ${port}`) })
能够用以下命令启动:npm
$ node index.js
这里要注意:json
requestHandler
: 每次请求到达服务器时,该函数都会被调用。若是从浏览器访问 localhost:3000
,就会出现两条日志信息:一条是 /
,一条是 favicon.ico
。api
if (err)
: 错误处理 - 若是端口已被占用,或者服务器由于其它缘由不能启动,就会在这里获得通知浏览器
http
模块是很低层的 - 用上面的代码片断建立复杂的 Web 应用程序是很耗时间的。这就是为何咱们常常会为项目选用一个框架的缘由。有不少框架能够选,可是最重要的是这些:服务器
本章和下一章咱们打算用 Express,由于在 NPM 上能够找到的 Express 模块最多。
快速、开放、极简的 Node.js Web 框架 - http://expressjs.com/
将 Express 添加到项目中的惟一方法是 NPM 安装:
$ npm install express --save
安装完 Express 后,咱们来看看如何建立一个像之前那样的应用程序:
const express = require('express') const app = express() const port = 3000 app.get('/', (request, response) => { response.send('Hello from Express!') }) app.listen(port, (err) => { if (err) { return console.log('something bad happened', err) } console.log('server is listening on ${port}') })
这里你必须注意到的最大区别是,Express 默认给了一个路由器。不须要手动检测 URL 来判断要作什么,而是用 app.get
、app.post
、app.put
等定义应用程序的路由。它们会被翻译为对应的 HTTP 动词。
Express 实现的最强大概念之一是中间件模式。
能够把中间件看成是 Unix 管道,可是是对 HTTP 请求的管道。
在图中你能够看到一个请求是如何经过 Express 应用程序的。它经历了三个中间件。每一个中间件均可以修改它,而后基于业务逻辑,要么第三个中间件送回一个响应,要么将它送到一个路由处理器。
在实践中,能够按这种方式作:
const express = require('express') const app = express() app.use((request, response, next) => { console.log(request.headers) next() }) app.use((request, response, next) => { request.chance = Math.random() next() }) app.get('/', (request, response) => { response.json({ chance: request.chance }) }) app.listen(3000)
这里要注意:
app.use
: 在这里定义中间件 - 它带有三个参数的函数,第一个是请求,第二个是响应,第三个是 next
回调。调用 next
就是通知 Express 能够跳到下一个中间件,或者路由处理器。
第一个中间件只是记录请求头,而后当即调用下一个。
第二个中间件给请求添加一个特殊属性 - 这是中间件模式最强大的功能之一。你的中间件能够向 request 对象添加额外的数据,这个数据能够被下游中间件读取或修改。
在全部框架中,正确的错误处理是相当重要的。在 Express 中,必须建立特殊的中间件函数来实现 - 一个带有四个参数的中间件:
const express = require('express') const app = express() app.get('/', (request, response) => { throw new Error('oops') }) app.use((err, request, response, next) => { // log the error, for now just console.log console.log(err) response.status(500).send('Something broke!') })
这里要注意:
错误处理函数应该是用 app.use
添加的最后一个函数。
错误处理器有一个 next
回调 - 它能够用来将多个错误处理器链在一块儿。
至此,咱们已经了解了如何发送 JSON 响应 - 是时候学习如何用简单方法渲染 HTML 了。为此,咱们要使用 express-handlebars 中的 handlebars 包。
首先,建立以下的目录结构:
├── index.js └── views ├── home.hbs └── layouts └── main.hbs
以后,用以下代码片断填充 index.js
:
// index.js const path = require('path') const express = require('express') const exphbs = require('express-handlebars') const app = express() app.engine('.hbs', exphbs({ defaultLayout: 'main', extname: '.hbs', layoutsDir: path.join(__dirname, 'views/layouts') })) app.set('view engine', '.hbs') app.set('views', path.join(__dirname, 'views'))
上述代码初始化 handlebars
引擎,将布局目录设置为 views/layouts
。布局将会存在这个目录中。
设置好后,能够把初始 html
放进 main.hbs
中 - 为保持简单,咱们就用这个:
<html> <head> <title>Express handlebars</title> </head> <body> {{{body}}} </body> </html>
这里的 {{{body}}}
占位符,就是放内容的地方 - 下面咱们建立 home.hbs
!
<h2>Hello {{name}}<h2>
要让它起做用,咱们得作最后一件事情:给 Express 应用程序添加一个路由处理器:
app.get('/', (request, response) => { response.render('home', { name: 'John' }) })
render
方法带有两个参数:
第一个是视图的名称;
第二个是要渲染的数据。
一旦调用这个端点,会获得以下的 HTML:
<html> <head> <title>Express handlebars</title> </head> <body> <h2>Hello John<h2> </body> </html>
这只是冰山一角 - 要学习如何添加更多布局甚至局部模板,请参考官方 express-handlebars 文档。
在某些状况下,你可能须要看到应用程序正在运行时 Express 发生了什么。为此,能够传递以下环境变量给 Express:DEBUG=express*
。
必须用以下方式启动 Node.js HTTP 服务器:
$ DEBUG=express* node index.js
本章学习了如何从头开始建立第一个 Node.js HTTP 服务器。我推荐用 Express 开始,而后随意去尝试。
下章会学习如何从数据库获取信息。