How to run html file using node js

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

    This is a simple html file "demo.htm" stored in the same folder as the node.js file.

    
    
      
        

    Heading

    Paragraph.

    Below is the node.js file to call this html file.

    var http = require('http');
    var fs = require('fs');
    
    var server = http.createServer(function(req, resp){
      // Print the name of the file for which request is made.
      console.log("Request for demo file received.");
      fs.readFile("Documents/nodejs/demo.html",function(error, data){
        if (error) {
          resp.writeHead(404);
          resp.write('Contents you are looking for-not found');
          resp.end();
        }  else {
          resp.writeHead(200, {
            'Content-Type': 'text/html'
          });
          resp.write(data.toString());
          resp.end();
        }
      });
    });
    
    server.listen(8081, '127.0.0.1');
    
    console.log('Server running at http://127.0.0.1:8081/');
    

    Intiate the above nodejs file in command prompt and the message "Server running at http://127.0.0.1:8081/" is displayed.Now in your browser type "http://127.0.0.1:8081/demo.html".

提交回复
热议问题