學(xué)習(xí)node.js 開(kāi)篇 1、安裝node.js,在官網(wǎng)上載安裝包,同時(shí)也要在命令行中查看版本 命令:
結(jié)果:
2、終端命令行中輸出node.js中文本
3、創(chuàng)建第一個(gè)node.js應(yīng)用
3.1 練習(xí):
var http = require(“http”);
http.createServer(function(){
response.write(200,{“Content-Type”:”text/plain"});
response.write(“Hello World!”);
response.end();
}).listen(1337,”127.0.0.1”);
console.log(“Server running at http://127.0.0.1:1337/“);
3.2 編輯練習(xí):
4、讀取文件服務(wù)
4.1 源碼
var fs = require(‘fs’);
var data = fs.readFileSync(‘input.txt’);
console.log(data.toString());
console.log('程序執(zhí)行結(jié)束!');
4.2 文件內(nèi)容
4.3 編輯練習(xí):
5、寫出Html源碼
5.1 源碼:
var http = require(‘http’);
http.createServer(function(){
var reHtml = “<html><head><title>Node.js Test</title><body>Hi Node,I like you so much</body></head></html>”;
res.writeHead({‘Content-Type’:’text/html'});
res.end(reHtml);
}).listen(1337,’127.0.0.1’);
console.log(’Server running at http://127.0.0.1:1337/');
5.2 編輯練習(xí):