Using node.js as a simple web server

后端 未结 30 2087
感情败类
感情败类 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

    0 讨论(0)
  • 2020-11-22 03:07

    There are already some great solutions for a simple nodejs server. There is a one more solution if you need live-reloading as you made changes to your files.

    npm install lite-server -g
    

    navigate your directory and do

    lite-server
    

    it will open browser for you with live-reloading.

    0 讨论(0)
  • 2020-11-22 03:08

    A slightly more verbose express 4.x version but that provides directory listing, compression, caching and requests logging in a minimal number of lines

    var express = require('express');
    var compress = require('compression');
    var directory = require('serve-index');
    var morgan = require('morgan'); //logging for express
    
    var app = express();
    
    var oneDay = 86400000;
    
    app.use(compress());
    app.use(morgan());
    app.use(express.static('filesdir', { maxAge: oneDay }));
    app.use(directory('filesdir', {'icons': true}))
    
    app.listen(process.env.PORT || 8000);
    
    console.log("Ready To serve files !")
    
    0 讨论(0)
  • 2020-11-22 03:10

    Check out this gist. I'm reproducing it here for reference, but the gist has been regularly updated.

    Node.JS static file web server. Put it in your path to fire up servers in any directory, takes an optional port argument.

    var http = require("http"),
        url = require("url"),
        path = require("path"),
        fs = require("fs"),
        port = process.argv[2] || 8888;
    
    http.createServer(function(request, response) {
    
      var uri = url.parse(request.url).pathname
        , filename = path.join(process.cwd(), uri);
    
      fs.exists(filename, function(exists) {
        if(!exists) {
          response.writeHead(404, {"Content-Type": "text/plain"});
          response.write("404 Not Found\n");
          response.end();
          return;
        }
    
        if (fs.statSync(filename).isDirectory()) filename += '/index.html';
    
        fs.readFile(filename, "binary", function(err, file) {
          if(err) {        
            response.writeHead(500, {"Content-Type": "text/plain"});
            response.write(err + "\n");
            response.end();
            return;
          }
    
          response.writeHead(200);
          response.write(file, "binary");
          response.end();
        });
      });
    }).listen(parseInt(port, 10));
    
    console.log("Static file server running at\n  => http://localhost:" + port + "/\nCTRL + C to shutdown");
    

    Update

    The gist does handle css and js files. I've used it myself. Using read/write in "binary" mode isn't a problem. That just means that the file isn't interpreted as text by the file library and is unrelated to content-type returned in the response.

    The problem with your code is you're always returning a content-type of "text/plain". The above code does not return any content-type, but if you're just using it for HTML, CSS, and JS, a browser can infer those just fine. No content-type is better than a wrong one.

    Normally the content-type is a configuration of your web server. So I'm sorry if this doesn't solve your problem, but it worked for me as a simple development server and thought it might help some other people. If you do need correct content-types in the response, you either need to explicitly define them as joeytwiddle has or use a library like Connect that has sensible defaults. The nice thing about this is that it's simple and self-contained (no dependencies).

    But I do feel your issue. So here is the combined solution.

    var http = require("http"),
        url = require("url"),
        path = require("path"),
        fs = require("fs")
        port = process.argv[2] || 8888;
    
    http.createServer(function(request, response) {
    
      var uri = url.parse(request.url).pathname
        , filename = path.join(process.cwd(), uri);
    
      var contentTypesByExtension = {
        '.html': "text/html",
        '.css':  "text/css",
        '.js':   "text/javascript"
      };
    
      fs.exists(filename, function(exists) {
        if(!exists) {
          response.writeHead(404, {"Content-Type": "text/plain"});
          response.write("404 Not Found\n");
          response.end();
          return;
        }
    
        if (fs.statSync(filename).isDirectory()) filename += '/index.html';
    
        fs.readFile(filename, "binary", function(err, file) {
          if(err) {        
            response.writeHead(500, {"Content-Type": "text/plain"});
            response.write(err + "\n");
            response.end();
            return;
          }
    
          var headers = {};
          var contentType = contentTypesByExtension[path.extname(filename)];
          if (contentType) headers["Content-Type"] = contentType;
          response.writeHead(200, headers);
          response.write(file, "binary");
          response.end();
        });
      });
    }).listen(parseInt(port, 10));
    
    console.log("Static file server running at\n  => http://localhost:" + port + "/\nCTRL + C to shutdown");
    
    0 讨论(0)
  • 2020-11-22 03:10

    I use below code to start a simple web server which render default html file if no file mentioned in Url.

    var http = require('http'),
    fs = require('fs'),
    url = require('url'),
    rootFolder = '/views/',
    defaultFileName = '/views/5 Tips on improving Programming Logic   Geek Files.htm';
    
    
    http.createServer(function(req, res){
    
        var fileName = url.parse(req.url).pathname;
        // If no file name in Url, use default file name
        fileName = (fileName == "/") ? defaultFileName : rootFolder + fileName;
    
        fs.readFile(__dirname + decodeURIComponent(fileName), 'binary',function(err, content){
            if (content != null && content != '' ){
                res.writeHead(200,{'Content-Length':content.length});
                res.write(content);
            }
            res.end();
        });
    
    }).listen(8800);
    

    It will render all js, css and image file, along with all html content.

    Agree on statement "No content-type is better than a wrong one"

    0 讨论(0)
  • 2020-11-22 03:10

    I can also recommend SugoiJS, it is very easy to set up and gives an option to start writing fast and has great features.

    Take a look here to get started: http://demo.sugoijs.com/ , documentation: https://wiki.sugoijs.com/

    It has request handling decorators, request policies and authorization policies decorators.

    For example:

    import {Controller,Response,HttpGet,RequestParam} from "@sugoi/server";
    ​
    @Controller('/dashboard')
    export class CoreController{
        constructor(){}
    ​
        @HttpGet("/:role")
        test(@RequestParam('role') role:string,
             @RequestHeader("role") headerRole:string){
            if(role === headerRole )
                return "authorized";
            else{
                throw new Error("unauthorized")
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题