Loading basic HTML in Node.js

后端 未结 20 1368
暗喜
暗喜 2020-11-28 17:34

I\'m trying to find out how to load and render a basic HTML file so I don\'t have to write code like:

response.write(\'...

blahblahblah

...\
20条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 18:19

    we can load the html document with connect frame work. I have placed my html document and the related images in the public folder of my project where the below code and node modules present.

    //server.js
    var http=require('http');
    var connect=require('connect');
    
    var app = connect()
      .use(connect.logger('dev'))
      .use(connect.static('public'))
      .use(function(req, res){
       res.end('hello world\n');
     })
    
    http.createServer(app).listen(3000);
    

    I have tried the readFile() method of fs, but it fails to load the images, that's why i have used the connect framework.

提交回复
热议问题