-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer.js
40 lines (32 loc) · 809 Bytes
/
Server.js
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
35
36
37
38
39
40
var http = require('http');
var fs = require("fs");
var path = require("path");
// var DataManager = require('./DataManager.js');
var dictTypes = {
'.js': 'text/javascript',
'.css': 'text/css',
}
http.createServer(function(req, res) {
mainHandle(req, res);
}).listen(2121, 'localhost');
console.log('Server running at localhost:2121');
function mainHandle(req, res) {
var fileName;
var file;
if (req.url === "/") {
fileName = "index.html";
} else {
fileName = req.url.substring(1, req.url.length);
}
console.log(fileName);
try {
file = fs.readFileSync(fileName);
} catch (e) {
console.log(e);
file = null;
}
var ext = path.extname(req.url) || ".html";
var contentType = dictTypes[ext] || 'text/html';
res.writeHead(200, {'Content-Type' : contentType});
res.end(file);
}