🌙 NodeJS之HelloWorld
🌙 NodeJS安装
略,详见nodejs官网网 (opens new window)
🌙 Hello NodeJS
// 导入http模块
var http = require("http");
// 创建服务器
var server = http.createServer(function(req, res) {
// 设置http头部
res.writeHead(200, {"Content-type": "text/html;charset=UTF-8"});
res.end("Hello NodeJS");
})
// 运行服务器
server.listen(6666);
console.log("server is running at http://localhost:6666");
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
🌙 NodeJS没有web容器的概念
NodeJS没有根目录的概念,它没有任何的web容器。它通过路由匹配,将对应的资源返回给客户端。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello node</title>
<link rel="stylesheet" href="./hahaha.css">
<style>
div {
width: 200px;
height: 200px;
background: bisque;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
index.css
body {
padding: 20px;
border: 1px dashed darkorange;
}
1
2
3
4
2
3
4
// 导入http模块
var http = require('http');
var fs = require('fs');
// 创建服务器
var server = http.createServer(function (req, res) {
if(req.url === '/index') {
fs.readFile('./index.html', function(err, data) {
res.writeHead(200, { 'Content-type': 'text/html;charset=UTF-8' });
res.end(data);
});
} else if(req.url === '/hahaha.css') {
fs.readFile('./index.css', function(err, data) {
// 更换content-type
res.writeHead(200, { 'Content-type': 'text/css;charset=UTF-8' });
res.end(data);
});
};
});
// 运行服务器
server.listen(7777);
console.log('server is running at http://localhost:7777/index');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
URL和真实文件是没有关系的,URL是通过了Node的顶层路由设计,呈递某一个静态文件的。