How to run html file using node js

前端 未结 8 844
南笙
南笙 2021-01-30 11:04

I have a simple html page with angular js as follows:

    //Application name
    var app = angular.module(\"myTmoApppdl\", []);

    app.controller(\"myCtrl\", f         


        
相关标签:
8条回答
  • 2021-01-30 11:24

    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"

    0 讨论(0)
  • 2021-01-30 11:25

    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

    0 讨论(0)
  • 2021-01-30 11:27

    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

    0 讨论(0)
  • 2021-01-30 11:34

    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

    0 讨论(0)
  • 2021-01-30 11:34

    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

    0 讨论(0)
  • 2021-01-30 11:39

    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:

    • run node init in root of app (this will create a package.json file)
    • install express in root of app : npm install --save express (save will update package.json with express dependency)
    • create a public folder in root of your app and put your entry point file (index.html) and all its dependent files (this is just for simplification, in large application this might not be a good approach).
    • Create a server.js file in root of app where in we will use express module of node which will serve the public folder from its current directory.
    • 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

    0 讨论(0)
提交回复
热议问题