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

后端 未结 4 1952
失恋的感觉
失恋的感觉 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:17

    Following code worked for me.

    var express = require('express'),
      app = express(),
      http = require('http'),
      httpServer = http.Server(app);
    
    app.use(express.static(__dirname + '/folder_containing_assets_OR_scripts'));
    
    app.get('/', function(req, res) {
      res.sendfile(__dirname + '/index.html');
    });
    app.listen(3000);
    

    this loads page with assets

提交回复
热议问题