How to run html file using node js

前端 未结 8 869
南笙
南笙 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: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

提交回复
热议问题