How to run html file using node js

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

提交回复
热议问题