Server capable of serving files
This commit is contained in:
commit
d6d5f85d1a
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/downloads/
|
||||||
|
*index-*.html
|
||||||
|
/node_modules/
|
13
package-lock.json
generated
Normal file
13
package-lock.json
generated
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "sellery",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"lockfileVersion": 1,
|
||||||
|
"requires": true,
|
||||||
|
"dependencies": {
|
||||||
|
"http": {
|
||||||
|
"version": "0.0.1-security",
|
||||||
|
"resolved": "https://registry.npmjs.org/http/-/http-0.0.1-security.tgz",
|
||||||
|
"integrity": "sha512-RnDvP10Ty9FxqOtPZuxtebw1j4L/WiqNMDtuc1YMH1XQm5TgDRaR1G9u8upL6KD1bXHSp9eSXo/ED+8Q7FAr+g=="
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
package.json
Normal file
15
package.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"name": "sellery",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "A password-secured webserver",
|
||||||
|
"main": "server.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"start": "node server.js"
|
||||||
|
},
|
||||||
|
"author": "Leon Etienne",
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"dependencies": {
|
||||||
|
"http": "0.0.1-security"
|
||||||
|
}
|
||||||
|
}
|
60
server.js
Executable file
60
server.js
Executable file
@ -0,0 +1,60 @@
|
|||||||
|
#!/home/menethil/.nvm/versions/node/v14.16.1/bin/node
|
||||||
|
|
||||||
|
var http = require('http');
|
||||||
|
var fs = require('fs');
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
|
const contentTypes = {
|
||||||
|
'.html': 'text/html',
|
||||||
|
'.css': 'text/css',
|
||||||
|
'.js': 'application/javascript',
|
||||||
|
'.jpg': 'image/jpeg',
|
||||||
|
'.jpeg': 'image/jpeg',
|
||||||
|
'.png': 'image/png',
|
||||||
|
'.webp': 'image/webp',
|
||||||
|
'.bmp': 'image/bmp',
|
||||||
|
'.svg': 'image/svg+xml',
|
||||||
|
'.ico': 'image/x-icon',
|
||||||
|
'.mp4': 'video/mp4',
|
||||||
|
'.webm': 'video/webm',
|
||||||
|
};
|
||||||
|
|
||||||
|
var server = http.createServer(function (request, response) {
|
||||||
|
// Handle requests here...
|
||||||
|
|
||||||
|
// Fetch requested file
|
||||||
|
fs.readFile(__dirname + request.url, function (error, data) {
|
||||||
|
if(!error) {
|
||||||
|
const mimetype = path.extname(request.url);
|
||||||
|
if (!(typeof mimetype === 'undefined')) {
|
||||||
|
|
||||||
|
response.writeHead(200, {
|
||||||
|
'Content-Type': mimetype
|
||||||
|
});
|
||||||
|
response.end(data);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
response.writeHead(500, {
|
||||||
|
'Content-Type': 'text/html'
|
||||||
|
});
|
||||||
|
|
||||||
|
console.error('Unknown file mime type for file: ' + __dirname + request.url);
|
||||||
|
response.end('Unknown file mime type.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
response.writeHead(404, {
|
||||||
|
'Content-Type': 'text/html'
|
||||||
|
});
|
||||||
|
|
||||||
|
console.error('File not found: ' + JSON.stringify(error));
|
||||||
|
response.end('File not found.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const port = 80;
|
||||||
|
server.listen(port);
|
||||||
|
console.log('Node.js sellery server running and listening to port ' + port);
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user