How to create a simple html server using express js

前端 未结 3 2119
青春惊慌失措
青春惊慌失措 2020-12-30 11:15

I\'m new in node.js I want to create a simple express.js static file server, but I have some issues. I have been installed express.js 4.2 globally like this:



        
相关标签:
3条回答
  • 2020-12-30 11:35

    This problem shouldn't even need any code or frameworks; install http-server from npm, navigate to the folder in the command prompt and run this command:

    http-server
    

    And it will spin up a lightweight http server and serve static content up from the folder immediately which can be viewed using http://localhost

    0 讨论(0)
  • 2020-12-30 11:36

    first install express module inspite of express-generator

    npm install express
    

    try this removing public

    var express = require('express');
    var app = express();
    
    app.use('/', express.static(__dirname));
    app.listen(3000, function() { console.log('listening')});
    

    it is working fine.

    0 讨论(0)
  • 2020-12-30 11:37

    If you're just trying to serve static files from a directory called "public", you might have luck with an app like this:

    var path = require('path');
    var express = require('express');
    
    var app = express();
    
    var staticPath = path.join(__dirname, '/public');
    app.use(express.static(staticPath));
    
    app.listen(3000, function() {
      console.log('listening');
    });
    

    You'll need to make sure Express is installed. You'll probably run npm install express --save in the same directory as the above JavaScript file. Once you're all ready, you'll run node the_name_of_the_file_above.js to start your server.

    0 讨论(0)
提交回复
热议问题