forked from gyrate/study-arcgis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
34 lines (31 loc) · 936 Bytes
/
app.js
File metadata and controls
34 lines (31 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//加载必须的模块
var http = require('http')
var fs = require('fs')
var url = require('url')
var path = require('path')
//定位静态目录的位置,根据请求找出对应的文件
function staticRoot(staticPath, req, res) {
var pathObj = url.parse(req.url, true)
if (pathObj.pathname === '/') {
pathObj.pathname += 'index.html'
}
//读取静态目录里面的文件,然后发送出去
var filePath = path.join(staticPath, pathObj.pathname)
fs.readFile(filePath, 'binary', function (err, content) {
if (err) {
res.writeHead(404, 'Not Found')
res.end('<h1>404 Not Found</h1>')
} else {
res.writeHead(200, 'Not Found')
res.write(content, 'binary')
res.end()
}
})
}
//创建服务器
var server = http.createServer(function (req, res) {
staticRoot(path.join(__dirname, ''), req, res)
})
//监听8080端口
server.listen(9090)
console.log('http://localhost:9090')