1. Node is specifically focused on solving the problem of building network applications—that is, applications that do a lot of input/output (I/O).
2. The net module contains all the TCP stuff for Node. From that, we can create a TCP server by calling the net.createServer() method. We add an event listener by using the on(“connection”, handler(client) ) method. Whenever the connection event happens, the event listener will call the handler function we gave it. A connection event happens when a new client connects to the server. The connection event passes us a reference to the TCP socket for our new client when it calls our callback function. By calling Socket.write, we can send messages to the newly connected client. Call the Socket.end method to close the connection. We need to call listen() on the server so Node knows which port to listen on. The data event is the event that is called each time client sends some data to the server, you can call Socket.on(“data”, process(data)) to add event handler for it. Node simply stores the data as binary until we ask for it in some other kind of encoding, if you use console.log to print it, the bytes in hex will be printed. We can use the toString() method to translate Buffer data into a regular string. When a socket disconnects, it fires the end event to indicate that it’s about to close. Socket.writable indicates whether the socket is available for writing and Socket.destroy close and destroy the socket object. error event on Socket will be triggered if clients throws any error.
3. Example of a chat server:
var net = require('net') var chatServer = net.createServer(), clientList = [] chatServer.on('connection', function(client) { client.name = client.remoteAddress + ':' + client.remotePort client.write('Hi ' + client.name + '!\n'); console.log(client.name + ' joined') clientList.push(client) client.on('data', function(data) { broadcast(data, client) }) client.on('end', function() { console.log(client.name + ' quit') clientList.splice(clientList.indexOf(client), 1) }) client.on('error', function(e) { console.log(e) }) }) function broadcast(message, client) { var cleanup = [] for(var i=0;i<clientList.length;i+=1) { if(client !== clientList[i]) { if(clientList[i].writable) { clientList[i].write(client.name + " says " + message) } else { cleanup.push(clientList[i]) clientList[i].destroy() } } } //Remove dead Nodes out of write loop to avoid trashing loop index for(i=0;i<cleanup.length;i+=1) { clientList.splice(clientList.indexOf(cleanup[i]), 1) } } chatServer.listen(9000)
4. The Express module is a web framework for Node that makes it much easier to create web applications by adding support for common tasks, such as MVC, to the existing http server.
5. Instead of including the http module, we include express module. Express is actually getting http behind the scenes. We call createServer() to make a server and call listen() to make it listen to a specific port. Instead of attaching an event listener to the request event, we can call methods matching the HTTP verbs. When we call get(), we can create a callback function that will match GET requests only to a URL that matches the first argument of the call.
6. Express provides a convenience method on the response (http.response) object named send(), and this method issues both the HTTP headers as well as a response.end() call:
var app = express.createServer() app.get('/', function(req, res) { res.send('Welcome to Node Twitter') }) app.listen(8000)
7. In JavaScript everything happens in an event loop. That means new events don’t get called until we’ve finished evaluating the code of the existing loop pass.
8. The server.listen() call is actually asynchronous because binding to a TCP port takes time. The addition of event listeners (via server.get() and server.post()) is synchronous.
9. A middleware is a small piece of code that sits in between the original request event and the route we defined with server.post(). We use middleware to reuse code for common tasks such as authentication or logging. One middleware included in Express is called bodyParser. This middleware’s job is to stream the POST data from the client and then turn it into a JavaScript object that we can use. We simply include it by specifying it in the arguments we give to the server.post() route. Notice that we call express.bodyParser(). This function call actually returns another function. We use this standard style for middleware to allow you to pass configuration to the middleware if you want to. If we didn’t include the middleware, we would have to manually write code to accept the data event provided by the request object. Only after we had streamed in all the POST data could we call the code in the server.post() route.
10. The express.bodyParser adds a property to request called request.body. This property (if it exists) contains an object representing the POST data. The express.bodyParser middleware will work only for POST requests with the content-type HTTP header of application/x-www-form-urlencoded (that’s what a web page form would send.)or application/json. Both of these are easy to parse into key/value pairs as properties of the request.body object.
11. If we give response.send() an object ( such as {status:”ok”, message:”done”}), it automatically serializes it as JSON and sends the correct HTTP headers.
12. assert is a core module in Node that lets us test return values in various ways. When a value doesn’t match the expected criteria, an exception is thrown.
13. The http library doesn’t just contain objects to serve HTTP; it also provides a client. The http.request() factory creates a new http.Request object. The http.request() constructor takes two arguments: the first is the config object, and the second is a callback. The config is a configuration object we pass that has a list of properties including the hostname, the port, URL path, HTTP method, and some HTTP headers. The callback is attached to the response event for the http.Request. It’s similar to an http.Server, except we have only one object in the response.
14. By using assert.strictEqual, we are checking that data matches the expected data using ===. If it doesn’t, an assert exception is thrown.
15. response.setEncoding() allows us to define the encoding of all the received data. By setting this to utf8, we ensure that any data we receive will be treated as the right kind of string.
16. In Express, we can use express.bodyDecoder to catch all the data in a request and stream it. But we don’t have the same luxury in the client, so we’ll do it by hand. We simply attach a function to the data event on response. Whenever data happens, we append it to our data variable.
17. We can listen for the end event of the response and then take further action on all of the data.
18. Calling write() on request lets us send data (if this is a POST request). We call end() to indicate that we are finished sending data with the request object.
19. A sample basic API of web app:
var express = require('express') var app = express.createServer() app.listen(8000) var tweets = [] app.get('/', function(req, res) { res.send('Welcome to Node Twitter') }) app.post('/send', express.bodyParser(), function(req, res) { if (req.body && req.body.tweet) { tweets.push(req.body.tweet) res.send({status:"ok", message:"Tweet received"}) } else { //no tweet? res.send({status:"nok", message:"No tweet received"}) } }) app.get('/tweets', function(req,res) { res.send(tweets) })
and corresponding test codes:
var http = require('http'), assert = require('assert') var opts = { host: 'localhost', port: 8000, path: '/send', method: 'POST', headers: {'content-type':'application/x-www-form-urlencoded'} } var req = http.request(opts, function(res) { res.setEncoding('utf8') var data = "" res.on('data', function(d) { data += d }) res.on('end', function() { assert.strictEqual(data, '{"status":"ok","message":"Tweet received"}') }) }) req.write('tweet=test') req.end()
20. Express supports an MVC (model, view, controller) approach oriented around the routing of requests. The routes act like controllers, providing a way to join the data model with a view.
21. Express offers a few different templating languages and is extensible to allow the addition of more.
22. EJS simply embeds JavaScript into the templates with a few simple tags to define how the JavaScript is interpreted. The JavaScript to be evaluated is between the <% and %> tags. You can simply list the variable or reference you wish to include in the page following = in the <% tags.
23. The layout file in Express defines a skeleton to use for your site. It’s some basic view boilerplate you will use almost everywhere. Partials are mini-templates for code that is expected to repeat again and again with different data. We use the layout template on all the pages on the site (unless we turn it off), we need some way to say where the specific template being rendered goes. Express provides the body variable for this task. This variable will contain the rendered contents of the specific template we load.
24. We use response.render() as the call to render a template. The first argument is the name of the specific template we want to render. Whatever is in the specific template will be rendered into the layout template where the body variable was. The second argument we pass to response.render() is a configuration object. The locals property of the configuration object contains the data used to render this template. All of these variables will be available to both the layout template and the page template.
25. If partials get passed an array, they will repeat for each item in the array. The variable each partial is using to access the array is the same as the name of the template.
26. An example :
EJS layout template:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <%- partial('partials/stylesheet', stylesheets) %> <title><%= title %></title> </head> <body> <h1><%= header %></h1> <%- body %> </body> </html>
The page(index) template:
<form action="/send" method="POST"> <input type="text" length="140" name="tweet"> <input type="submit" value="Tweet"> </form> <%- partial('partials/chirp', tweets) %>
The chirp partial:
<p><%= chirp %></p>
The stylesheet partial:
<link rel="stylesheet" type="text/css" href="<%- stylesheet %>">
Two routes:
app.post('/send', express.bodyParser(), function(req, res) { if (req.body && req.body.tweet) { tweets.push(req.body.tweet) if(acceptsHtml(req.headers['accept'])) { res.redirect('/', 302) } else { res.send({status:"ok", message:"Tweet received"}) } } else { //no tweet? res.send({status:"nok", message:"No tweet received"}) } }) function acceptsHtml(header) { var accepts = header.split(',') for(i=0;i<accepts.length;i+=0) { if (accepts[i] === 'text/html') { return true } } return false } app.get('/', function(req, res) { var title = 'Chirpie', header = 'Welcome to Chirpie' res.render('index', { locals: { 'title': title, 'header': header, 'tweets': tweets, stylesheets: ['/public/style.css'] } }) })
相关推荐
Chapter 2, Something We Look At – Graphical User Interfaces, covers user interface capabilities for our OpenCV-based applications. Chapter 3, First Things First – Image Processing, covers the most ...
5. **Chapter 2: Doing Interesting Things**:通过实际示例展示如何使用Node.js进行Web开发,并介绍了一些基本的编程模式。 - [http://ofps.oreilly.com/titles/9781449398583/chap2a.html]...
在八年级英语上册Chapter 4 "Computer Technology"的学习案中,学生将深入了解计算机科学的基本概念,并通过英语学习这些知识。本章节的目标是让学生掌握一系列与计算机相关的专业词汇,同时也提升他们的英语语言...
Chapter 2 Definitions 2.1. Activity An “activity” is an element of work performed during the course of a project [PMI, 2000]. It has an output and leads towards an outcome. Such an output can ...
资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。
wrf转mp4播放器1.1.1
内容概要:本文档详细介绍了如何在Simulink中设计一个满足特定规格的音频带ADC(模数转换器)。首先选择了三阶单环多位量化Σ-Δ调制器作为设计方案,因为这种结构能在音频带宽内提供高噪声整形效果,并且多位量化可以降低量化噪声。接着,文档展示了具体的Simulink建模步骤,包括创建模型、添加各个组件如积分器、量化器、DAC反馈以及连接它们。此外,还进行了参数设计与计算,特别是过采样率和信噪比的估算,并引入了动态元件匹配技术来减少DAC的非线性误差。性能验证部分则通过理想和非理想的仿真实验评估了系统的稳定性和各项指标,最终证明所设计的ADC能够达到预期的技术标准。 适用人群:电子工程专业学生、从事数据转换器研究或开发的技术人员。 使用场景及目标:适用于希望深入了解Σ-Δ调制器的工作原理及其在音频带ADC应用中的具体实现方法的人群。目标是掌握如何利用MATLAB/Simulink工具进行复杂电路的设计与仿真。 其他说明:文中提供了详细的Matlab代码片段用于指导读者完成整个设计流程,同时附带了一些辅助函数帮助分析仿真结果。
国网台区终端最新规范
《基于YOLOv8的智慧农业水肥一体化控制系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计
GSDML-V2.33-LEUZE-AMS3048i-20170622.xml
微信小程序项目课程设计,包含LW+ppt
微信小程序项目课程设计,包含LW+ppt
终端运行进度条脚本
幼儿园预防肺结核教育培训课件资料
python,python相关资源
《基于YOLOv8的智慧校园电动车充电桩状态监测系统》(包含源码、可视化界面、完整数据集、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计
deepseek 临床之理性软肋.pdf
SM2258XT量产工具(包含16种程序),固态硬盘量产工具使用
RecyclerView.zip
水务大脑让水务运营更智能(23页)