[Node.js] Level 1 new. Intro the Node.js

1.2 Hello You

Let's start with a simple Hello server. Follow the tasks below to create a simple Node server that outputs a greeting.

First, tell the response which status it should have (a successful status is200).

Next, write a message to the response body in the form of "Hello, this is <your name here>".

To finish it up, tell the response to end so the client on the other side knows it has received all the data.

var http = require('http');

http.createServer(function(request, response) {
  response.writeHead(200);
  response.write("Hello, this is Zhentian");
  response.end();
}).listen(8080);

 

1.3 Convert Blocking

Not everyone knows why it's important to write non-blocking programs in Node.js. One of these unfortunate souls has written some code to read a file off the file-system using the blocking function readFileSync. Convert the code to be non-blocking using the readFile function instead.

Start by changing the call from readFileSync() to readFile().

Next, add a callback method to the readFile() call. This method should accept error and contents parameters.

To finish it up, remove the contents var declaration, and move the call toconsole.log() inside your callback.

var fs = require('fs');

fs.readFile('index.html', function(err, contents){
    console.log(contents);
});

 

1.4 Running Your Code

While you could go to the website and easily install node, we happen to have a console below where you can practice running node applications.

Go ahead and run that file we just created to read a file off the filesystem with
node file_read.js

$ node file_read.js

 

1.5 Read File in Server

Now that you know how to create an HTTP server and how to read a file off the filesystem in a non-blocking way, let's try to combine the two.

Instead of just writing a string to the HTTP response, write the contents of index.html to the response instead.

After response.writeHead(200), add a call to fs.readFile() that readsindex.html asynchronously. Remember to pass a callback function, that accepts an error parameter, and a contents parameter.

Now that you have the file contents, write it to the response.

To finish up, end the response after the file contents have been written.

复制代码
var http = require('http');
var fs = require('fs');

http.createServer(function(request, response) {
  response.writeHead(200);
  fs.readFile('index.html', function(error, contents){
    response.write(contents);
    response.end();
  });
}).listen(8080);
复制代码

 

1.6 Issuing a Request

Let's see our new server in action. We've already run node app.js, so in the terminal below use curl to issue a request to http://localhost:8080 and we'll see our server respond with the contents of index.html.

$ curl http://localhost:8080

---------
<html><p>Hello, this is Dog</p></html>

 

1.7 Writing Response Headers

Up until now all we've been sending into the response.writeHead()function is the status code. However, it can take additional parameters.

Consult the node documentation, and add a 'Content-Type' of 'text/html' to the response.

复制代码
var http = require('http');
var fs = require('fs');

http.createServer(function(request, response) {
  response.writeHead(200, {
    'Content-Type': 'text/html'
  });

  fs.readFile('index.html', function(err, contents) {
    response.write(contents);
    response.end();
  });

}).listen(8080);
复制代码

 

1.8 Response End

Our original Hello server can be shortened since theresponse.end() function optionally takes data as a parameter. Remove the 'response.write' line altogether, and send the hello string as a parameter on the response.end function. This will send the data, and once finished add the end to the response.

Instead of passing the content to response.write(), pass it toresponse.end().

var http = require('http');

http.createServer(function(request, response) {
  response.writeHead(200);
  response.end("Hello, this is dog");
}).listen(8080);

 

posted @   Zhentiw  阅读(728)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示