-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpHOServer.js
111 lines (92 loc) · 3.48 KB
/
pHOServer.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Description: Simple and lite Dev HTTP server made with Node.js that doesn't require any configuration.
* Ideal to test or show (make a demonstration) of a static Web project.
*
* Author: Pierre-Henry Soria <[email protected]>
* Copyright: (c) 2016-2017, Pierre-Henry Soria. All Rights Reserved.
* License: MIT License <http://opensource.org/licenses/mit-license.php>
* URL: https://github.com/pH-7
*/
'use strict';
/** Set the global constants **/
// Set the default post
const DEF_PORT = 8080;
// Set default server address
const ADDRESS = '127.0.0.1';
// Server name. You CANNOT change the name for copyright-law reason!
const SERVER_NAME = 'pHOServer - By Pierre-Henry Soria';
function pHOServer() {
var me = this; // Self Object
// Import HTTP node.js module
this.http = require('http');
/** Import other useful modules **/
this.fs = require('fs');
//var this.url = require('url'); // Maybe useful for later...
this.path = require('path');
this.port = process.argv[2] || DEF_PORT;
/*
* Set the correct content type
*/
this.getContentType = function(ext) {
var contentType;
switch (ext) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.jpg' || '.jpeg':
contentType = 'image/jpeg';
break;
case '.png':
contentType = 'image/png';
break;
case '.gif':
contentType = 'image/gif';
break;
case '.wav':
contentType = 'audio/wav';
break;
default:
contentType = 'text/html';
}
return contentType;
};
this.init = function() {
var server = this.http.createServer(function(request, response) {
// Get the full path requested
var pathname = '.' + request.url;
if (pathname === './') {
pathname = './index.html';
}
console.log("Request for " + pathname);
me.fs.readFile(pathname, function (error, data) {
if (error) {
console.log(error);
if (error.code == 'ENOENT') {
// HTTP Status: 404 : NOT FOUND
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end("404 - Page Not Found!\r\n");
} else {
response.writeHead(500, {'Content-Type': 'text/plain'});
response.end('Sorry, check with the site admin for error: ' + error.code + "\r\n");
}
} else {
var ext = me.path.extname(pathname);
var contentType = me.getContentType(ext);
// HTTP Status: 200 : OK
response.writeHead(200, {'Content-Type': contentType, 'X-Powered-By': SERVER_NAME, 'Server': SERVER_NAME});
response.end(data, 'utf-8');
}
});
});
server.listen(this.port, ADDRESS);
console.log("Awesome! pHOServer is running on: %s", ADDRESS + ':' + this.port);
};
}
var pHServer = new pHOServer;
pHServer.init();