Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const http = require('http');
- const fs = require('fs');
- const url = require('url');
- const folderPath = __dirname + '/public_files'
- http.createServer( function(request, response) {
- var baseURI = url.parse(request.url)
- var filePath = baseURI.pathname === '/' ? folderPath + '/index.html' : folderPath + baseURI.pathname
- fs.access(filePath, fs.F_OK | fs.R_OK, function(err){
- if (err) {
- response.writeHead(404, {'Content-Type':'text/html'})
- response.end('<h1>File not found</h1>')
- } else {
- fs.readFile(filePath, function(err, contenFile){
- if(!err) {
- response.writeHead(200, {'Content-Type':'text/html'})
- response.end(contenFile)
- } else {
- response.writeHead(500, {'Content-Type':'text/html'})
- response.end('<h1>Cannot read this content</h1>')
- }
- });
- }
- });
- } ).listen(3000);
Advertisement
Add Comment
Please, Sign In to add comment