-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicWebServer.js
More file actions
37 lines (36 loc) · 1.12 KB
/
Copy pathBasicWebServer.js
File metadata and controls
37 lines (36 loc) · 1.12 KB
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
'use strict';
var http = require('http'),
fs = require('fs'),
path = require('path'),
host = '127.0.0.1',
port = '9000',
mimes = {
'.htm': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript',
'.jpg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif'
};
http.createServer(function(req, res) {
var filePath = (req.url === '/') ? './index.htm' : '.' + req.url,
contentType = mimes[path.extname(filePath)];
fs.exists(filePath, function(fileExists) {
if (fileExists) {
fs.readFile(filePath, function(error, content) {
if (error) {
res.writeHead(500);
res.end();
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
} else {
res.writeHead(404);
res.end('Sorry we could not find the file requested');
}
});
}).listen(port, host, function() {
console.log('Server running on http://' + host + ':' + port);
});