Using node.js as a simple web server

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

    You don't need to use any NPM modules to run a simple server, there's a very tiny library called "NPM Free Server" for Node:

    • NPM Free Server on GitHub

    50 lines of code, outputs if you are requesting a file or a folder and gives it a red or green color if it failed for worked. Less than 1KB in size (minified).

    0 讨论(0)
  • 2020-11-22 03:17
    var http = require('http');
    var fs = require('fs');
    var index = fs.readFileSync('index.html');
    
    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        // change the to 'text/plain' to 'text/html' it will work as your index page
        res.end(index);
    }).listen(9615);
    

    I think you where searching for this. In your index.html, simply fill it with normal html code - whatever you want to render on it, like:

    <html>
        <h1>Hello world</h1>
    </html>
    
    0 讨论(0)
  • 2020-11-22 03:18

    I'm not sure if this is exactly what you wanted, however, you can try changing:

    {'Content-Type': 'text/plain'}
    

    to this:

    {'Content-Type': 'text/html'}
    

    This will have the browser client display the file as html instead of plain text.

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

    Most of the answers above describe very nicely how contents are being served. What I was looking as additional was listing of the directory so that other contents of the directory can be browsed. Here is my solution for further readers:

    'use strict';
    
    var finalhandler = require('finalhandler');
    var http = require('http');
    var serveIndex = require('serve-index');
    var serveStatic = require('serve-static');
    var appRootDir = require('app-root-dir').get();
    var log = require(appRootDir + '/log/bunyan.js');
    
    var PORT = process.env.port || 8097;
    
    // Serve directory indexes for reports folder (with icons)
    var index = serveIndex('reports/', {'icons': true});
    
    // Serve up files under the folder
    var serve = serveStatic('reports/');
    
    // Create server
    var server = http.createServer(function onRequest(req, res){
        var done = finalhandler(req, res);
        serve(req, res, function onNext(err) {
        if (err)
            return done(err);
        index(req, res, done);
        })
    });
    
    
    server.listen(PORT, log.info('Server listening on: ', PORT));
    
    0 讨论(0)
  • 2020-11-22 03:19

    Step1 (inside command prompt [I hope you cd TO YOUR FOLDER]) : npm install express

    Step 2: Create a file server.js

    var fs = require("fs");
    var host = "127.0.0.1";
    var port = 1337;
    var express = require("express");
    
    var app = express();
    app.use(express.static(__dirname + "/public")); //use static files in ROOT/public folder
    
    app.get("/", function(request, response){ //root dir
        response.send("Hello!!");
    });
    
    app.listen(port, host);
    

    Please note, you should add WATCHFILE (or use nodemon) too. Above code is only for a simple connection server.

    STEP 3: node server.js or nodemon server.js

    There is now more easy method if you just want host simple HTTP server. npm install -g http-server

    and open our directory and type http-server

    https://www.npmjs.org/package/http-server

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

    The way I do it is to first of all install node static server globally via

    npm install node-static -g
    

    then navigate to the directory that contains your html files and start the static server with static.

    Go to the browser and type localhost:8080/"yourHtmlFile".

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