Node itself can serve static files without an expression or any other module ..?

I am new to node js. There is no idea how to send a simple request from url For example: - http: // localhost: 9999 / xyz / inde.html my file hierarchy

server.js xyz(folder)- |->index.html 

And get the html page from my server. What works for post 9999

 var http = require("http"); function onRequest(request, response) { console.log("Request received."); response.writeHead(200, {"Content-Type": "text/plain"}); response.end(); } http.createServer(onRequest).listen(9999); console.log("Server has started."); 

I know that I can send a string (with an html template) from a node js server and send it in response, but how to send a file without an expression and any other external module. Thanks

+5
source share
3 answers

It is ridiculous to try to create a node application without npm dependencies, because the nodejs base is just a base. If you don’t like the implementation of whole protocols, you are better off using a minimal, well-supported npm module that does this for you. However, here is the simplest thing you asked for (without MiME, eTags, caching, etc. Etc.):

 var basePath = __dirname; var http = require('http'); var fs = require('fs'); var path = require('path'); http.createServer(function(req, res) { var stream = fs.createReadStream(path.join(basePath, req.url)); stream.on('error', function() { res.writeHead(404); res.end(); }); stream.pipe(res); }).listen(9999); 
+4
source

its very simple, node already provides a fs module from which u can read this html file and add obj to the response as follows:

 response.writeHead(200, {"Content-Type": "text/plain"}); //here is the code required fs.readFile("./xyz/index.html", (err,fileContent) => { response.end(fileContent); }); 

but the problem here is that you only get the HTML document and not the resources inside the HTML files that are stored in different folders, for example, if you have this code in your index.html

<link rel="stylesheet" href="../css/index.css" />

this index.css will not be resolved by the node server. But I think your question is resolved.

0
source

 const http = require('http'); const fs = require("fs"); const path = require("path"); function send404(response){ response.writeHead(404, {'Content-Type': 'text/plain'}); response.write('Error 404: Resource not found.'); response.end(); } const mimeLookup = { '.js': 'application/javascript', '.html': 'text/html' }; const server = http.createServer((req, res) => { if(req.method == 'GET'){ let fileurl; if(req.url == '/'){ fileurl = 'index.html'; }else{ fileurl = req.url; } let filepath = path.resolve('./' + fileurl); let fileExt = path.extname(filepath); let mimeType = mimeLookup[fileExt]; if(!mimeType) { send404(res); return; } fs.exists(filepath, (exists) => { if(!exists){ send404(res); return; } res.writeHead(200, {'Content-Type': mimeType}); fs.createReadStream(filepath).pipe(res); }); } }).listen(3000); console.log("Server running at port 3000"); 
0
source

Source: https://habr.com/ru/post/1211584/


All Articles