I have a simple html page with angular js as follows:
//Application name
var app = angular.module(\"myTmoApppdl\", []);
app.controller(\"myCtrl\", f
Move your HTML file in a folder "www". Create a file "server.js" with code :
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/www'));
app.listen('3000');
console.log('working on 3000');
After creation of file, run the command "node server.js"
The simplest command by far:
npx http-server
This requires an existing index.html at the dir at where this command is being executed.
This was already mentioned by Vijaya Simha, but I thought using npx is way cleaner and shorter. I am running a webserver with this approach since months.
Docs: https://www.npmjs.com/package/http-server
http access and get the html files served on 8080:
>npm install -g http-server
>http-server
if you have public (./public/index.html) folder it will be the root of your server if not will be the one that you run the server. you could send the folder as paramenter ex:
http-server [path] [options]
expected Result:
*> Starting up http-server, serving ./public Available on:
http://LOCALIP:8080
http://127.0.0.1:8080
Hit CTRL-C to stop the server
http-server stopped.*
Now, you can run: http://localhost:8080
will open the index.html on the ./public folder
references: https://www.npmjs.com/package/http-server
You can use built-in nodejs web server.
Add file server.js
for example and put following code:
var http = require('http');
var fs = require('fs');
const PORT=8080;
fs.readFile('./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(PORT);
});
And after start server from console with command node server.js
. Your index.html page will be available on URL http://localhost:8080
Just install http-server globally
npm install -g http-server
where ever you need to run a html file run the command http-server
For ex: your html file is in /home/project/index.html
you can do /home/project/$ http-server
That will give you a link to accessyour webpages:
http-server
Starting up http-server, serving ./
Available on:
http://127.0.0.1:8080
http://192.168.0.106:8080
I too faced such scenario where I had to run a web app in nodejs with index.html being the entry point. Here is what I did:
node init
in root of app (this will create a package.json file)npm install --save express
(save will update package.json with express dependency)server.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public')); //__dir and not _dir
var port = 8000; // you can use any port
app.listen(port);
console.log('server on' + port);
do node server
: it should output "server on 8000"
start http://localhost:8000/ : your index.html will be called
File Structure would be something similar