Update dependencies and add blog

This commit is contained in:
Corentin 2021-10-07 09:02:43 +09:00
commit 7a8fb3449d
22 changed files with 948 additions and 332 deletions

View file

@ -1,9 +1,8 @@
const fs = require('fs');
const http = require('http');
const path = require('path');
const url = require('url');
const fs = require('fs')
const http = require('http')
const path = require('path')
const port = 8080; // Prefer port > 1024 to avoid super-user permission
const port = 8080 // Prefer port > 1024 to avoid super-user permission
const server = http.createServer((request, response) => {
const mimeType = {
@ -13,50 +12,58 @@ const server = http.createServer((request, response) => {
'.svg': 'image/svg+xml',
'.css': 'text/css',
'.json': 'application/json'
};
}
const mimeEncoding = {
'.gz': 'gzip',
};
let pathname = 'public' + url.parse(request.url).pathname;
fs.exists(pathname, function (exist) {
if(!exist)
}
let pathname = 'public' + decodeURI(request.url)
if(!fs.existsSync(pathname) || fs.statSync(pathname).isDirectory())
{
// If the file is not found, return 404
// response.statusCode = 404
// response.end(`File ${pathname} not found!`)
// return
pathname = 'public/index.html'
}
// If is a directory, then look for index.html
// else if (fs.statSync(pathname).isDirectory()) {
// pathname += 'index.html'
// }
if(!fs.existsSync(pathname))
{
// If the file is not found, return 404
response.statusCode = 404
response.end(`File ${pathname} not found!`)
return
}
// Read file from file system
fs.readFile(pathname, function(error, data){
if(error)
{
// If the file is not found, return 404
response.statusCode = 404;
response.end(`File ${pathname} not found!`);
return;
response.statusCode = 500
response.end(`Error getting the file: ${error}.`)
}
// If is a directory, then look for index.html
if (fs.statSync(pathname).isDirectory()) {
pathname += '/index.html';
else
{
// Based on the URL path, extract the file extention. e.g. .js, .doc, ...
const extension = path.parse(pathname).ext
// Set Content-Type
response.setHeader('Content-Type', mimeType[extension] || 'text/plain' )
// If the file is found, set Content-Encoding
if(mimeEncoding[extension])
{
response.setHeader('Content-Encoding', mimeEncoding[extension])
}
response.end(data)
}
// Read file from file system
fs.readFile(pathname, function(error, data){
if(error)
{
response.statusCode = 500;
response.end(`Error getting the file: ${error}.`);
}
else
{
// Based on the URL path, extract the file extention. e.g. .js, .doc, ...
const extension = path.parse(pathname).ext;
// Set Content-Type
response.setHeader('Content-Type', mimeType[extension] || 'text/plain' );
// If the file is found, set Content-Encoding
if(mimeEncoding[extension])
{
response.setHeader('Content-Encoding', mimeEncoding[extension]);
}
response.end(data);
}
});
});
});
})
})
server.listen(port, (error) => {
if (error) {
return console.log(`Server cannot listen port ${port} :`, error);
return console.log(`Server cannot listen port ${port} :`, error)
}
console.log(`Server is listening on port ${port}`);
});
console.log(`Server is listening on port ${port}`)
})