0

I'm sorry to ask such a simple question. I've been sent files by someone, an index.html file which pulls in a js file within script tags. I have to start a webserver to get through authentication and view the files (am in dev).

In my CLI i have navigated to the directory containing index.html. I have checked with node -v that I have it installed globally (yes, v 8.6). I've run the simple command node and checked my browser at http://localhost:3000 and a few other ports but get no joy. I've also tried node index.html but CLI throws an error.

How do i start the webserver? All the examples online tell me to build a .js file, but this is not an option.

0

4 Answers 4

3

Steps to set up a node web server

  1. Create the route folder from your local machine.
  2. Go to the command prompt from the project root path.
  3. Install express using the command npm install express
  4. Create server.js file
  5. create the folder wwww and create the Index.html inside it.

server.js

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/www'));

app.listen('3000');
console.log('working on 3000');

Index.html

<!doctype html
<html> 
<head> 
<title> my local server </title> 
</head>
<body>
<h1> server working </h1>
<p> just put your html,css, js files here and it work on your own local nodejs server </p>
</body>
</html>

Go to the project root path and take the command prompt, then start the server by running the command node server.js

Then go to the browser and run the url localhost:3000.

Now you can see the html page will render on your browser.

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

Comments

1

Since you don't want to build a backend but just an http server. I would propose to use an npm package that do just what you need:

Open a console

npm install http-server -g

Go to your "index.html" folder (in the console) then type:

http-server

Then reach your content in your browser at this address:

http://localhost:8080

Documentation here: https://www.npmjs.com/package/http-server

1 Comment

Thank you for this. Currently the download keeps timing out due to connectivity issues. I don't think I'm behind a proxy as I've used npm install successfully before. If you know about this subject i'd be grateful
1

Yes, this is possible.

A very simple example of how to do this would be to create file, let's call it app.js and put this in it:

const http = require('http'),   // to listen to http requests
      fs = require('fs');       // to read from the filesystem

const app = http.createServer((req,res) => {
    // status should be 'ok'
    res.writeHead(200); 

    // read index.html from the filesystem, 
    // and return in the body of the response
    res.end(fs.readFileSync("index.html")); 
});

app.listen(3000); // listen on 3000

Now, run node app.js

Browse to http://localhost:3000

There's loads of other npm packages that will help you out do this, but this is the simplest 'pure node' example to literally read index.html and serve it back as the response.

1 Comment

thanks for your reply. I need an extra backslash before the index.html. Currently CLI throws error looking for folderNameindex.html. I've tried various forward and backward slashes within the double quotes, plus os.tmpdir(). Any tips?
0

Its very easy to start a server using node js

Create a server.js file,

const http = require('http')
const fs = require('fs');

http.createServer(function (req, res) {
  fs.readFile('index.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });
}).listen(3000);

Run node server.js

Here is a reference

This will even solve your backslash issue by this

1 Comment

Glad to help you. :-)

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.