2

On my local machine, I want to deploy static Web pages (containing HTML, CSS, JS files) on Node, without using any framework (e.g., Express). I did put the Web page related files into the public folder, and then call the index.html, by using the fs library in node, as the following:

var http = require('http'), fs = require('fs');

fs.readFile('./public/index.html', function (err, html) {
  if (err) { throw err;}
  http.createServer(function(request, response) {
    response.writeHeader(200, {"Content-Type": "text/html"});
    response.write(html); response.end();
   }).listen(1337, '127.0.0.1');;
});

I used CURL and all the files (HTML, CSS, JS) are in fact deployed on the localhost. However, when I go to the port 1337 on the localhost, it shows the HTML contents but doesn't show the behavior, written in JS, imported in the index.html.

10
  • You server is set up on port 1337 and not 80. Did you access the correct port? Commented Aug 29, 2014 at 13:34
  • Go to 127.0.0.1:1337 Commented Aug 29, 2014 at 13:34
  • @Sirko Sorry, did a mistake on the port number, however I still do not get the behavior (written in a JS file in the public directory). Commented Aug 29, 2014 at 13:48
  • @ExplosionPills Updated the question. Cutting short, JS still is not responding. Commented Aug 29, 2014 at 13:49
  • Have a look at this question: Node.js http.createServer how to get error. Maybe the port is blocked or something similar? Commented Aug 29, 2014 at 13:51

1 Answer 1

4

Your index.html file is making a separate request to index.js (or whatever the JS file is / files are). Your server will handle every response the same way: serve the contents of the index.html file.

Creating a server in this way does not automatically serve files from the file system like other servers such as Apache might. Instead, you would need to do something like:

http.createServer(function(request, response) {
  if (request.url == "/index.html" || request.url == "/") {
    response.writeHeader(200, {"Content-Type": "text/html"});
    fs.createReadStream("./public/index.html").pipe(response);
  }
  else if (request.url == "/index.js") {
    response.writeHeader(200, {"Content-Type": "text/javascript"});
    fs.createReadStream("./public/index.js").pipe(response);
  }
}).listen(1337, '127.0.0.1');

Of course writing a route for each file in the file system that you want to serve is probably pretty silly. You could use a static file server module, or implement a static file server yourself. This would involve checking the file system based on the request URL and then serving the file with the correct content type which you can look up using a module like mime.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.