Basic webserver with node.js and express for serving html file and assets

后端 未结 4 1910
失恋的感觉
失恋的感觉 2021-01-30 06:37

I\'m making some frontend experiments and I\'d like to have a very basic webserver to quickly start a project and serve the files (one index.html file + some css/js/img files).

4条回答
  •  执笔经年
    2021-01-30 07:35

    You could use a solution like this in node.js (link no longer works), as I've blogged about before.

    The summarise, install connect with npm install connect.

    Then paste this code into a file called server.js in the same folder as your HTML/CSS/JS files.

    var util = require('util'),
        connect = require('connect'),
        port = 1337;
    
    connect.createServer(connect.static(__dirname)).listen(port);
    util.puts('Listening on ' + port + '...');
    util.puts('Press Ctrl + C to stop.');
    

    Now navigate to that folder in your terminal and run node server.js, this will give you a temporary web server at http://localhost:1337

提交回复
热议问题