819fa355f4
This patch: - converts all var's to let's throughout the codebase - enforces the let's over var's via the eslint rule
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
/**
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
let http = require('http');
|
|
let url = require('url');
|
|
let fs = require('fs');
|
|
let path = require('path');
|
|
let mime = require('mime');
|
|
|
|
class StaticServer {
|
|
/**
|
|
* @param {string} dirPath
|
|
* @param {number} port
|
|
*/
|
|
constructor(dirPath, port) {
|
|
this._server = http.createServer(this._onRequest.bind(this));
|
|
this._server.listen(port);
|
|
this._dirPath = dirPath;
|
|
}
|
|
|
|
stop() {
|
|
this._server.close();
|
|
}
|
|
|
|
_onRequest(request, response) {
|
|
let pathName = url.parse(request.url).path;
|
|
if (pathName === '/')
|
|
pathName = '/index.html';
|
|
pathName = path.join(this._dirPath, pathName.substring(1));
|
|
fs.readFile(pathName, function(err, data) {
|
|
if (err) {
|
|
response.statusCode = 404;
|
|
response.end(`File not found: ${pathName}`);
|
|
return;
|
|
}
|
|
response.setHeader('Content-Type', mime.lookup(pathName));
|
|
response.end(data);
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = StaticServer;
|