Using node.js as a simple web server

后端 未结 30 2195
感情败类
感情败类 2020-11-22 02:54

I want to run a very simple HTTP server. Every GET request to example.com should get index.html served to it but as a regular HTML page (i.e., same

30条回答
  •  盖世英雄少女心
    2020-11-22 03:05

    You can use Connect and ServeStatic with Node.js for this:

    1. Install connect and serve-static with NPM

      $ npm install connect serve-static
      
    2. Create server.js file with this content:

      var connect = require('connect');
      var serveStatic = require('serve-static');
      
      connect()
          .use(serveStatic(__dirname))
          .listen(8080, () => console.log('Server running on 8080...'));
      
    3. Run with Node.js

      $ node server.js
      

    You can now go to http://localhost:8080/yourfile.html

提交回复
热议问题