简体   繁体   中英

node-http-server module to respond with index.html to any request

I have installed a node-http-server module. I launch it from myDir on localhost port 8000. In myDir I have index.html. If I request (from the browser) http://localhost:8000/ I get index.html , which is OK and good.

If I request though http://localhost:8000/anything I do not get any response from the server.

Here's the Code I used (app.js):

const server=require('node-http-server');
var fs = require("fs");
var config = new server.Config;
config.root=__dirname;
config.server.index='index.html';

function getReq(req, res, body,enc) {
    console.log("got rewq");
    var content = fs.readFileSync('index.html', 'utf8');
    body.value = content;

}
server.deploy({port:8000});

I found that the same question asked in this link: node http-server to respond with index.html to any request . But it was for http-server module. I am using node-http-server module. I read documentation in https://github.com/RIAEvangelist/node-http-server . But can't find any suitable solutions. Help me with Some solutions.

To start exploring your problem i would look at your code first.

const server=require('node-http-server');
var fs = require("fs");
var config = new server.Config;
config.root=__dirname;
config.server.index='index.html';

function getReq(req, res, body,enc) {
    console.log("got rewq");
    var content = fs.readFileSync('index.html', 'utf8');
    body.value = content;

}
server.deploy({port:8000});

GetReq() function clearly should do what you expect it to do but i dont see where you actually supply node-http-server witht his function to be called as a callback. Now lets fix your code.

const server=require('node-http-server');
var fs = require("fs");
var config = new server.Config;
config.root=__dirname;
config.server.index='index.html';

function getReq(req, res, body,enc) {
    console.log("got rewq");
    var content = fs.readFileSync('index.html', 'utf8');
    body.value = content;

}
server.deploy({port:8000}, getReq);

In short you didnt read documentation for the module, you need to pass your callback function as an option to deploy method. server.deploy({port:8000}, getReq);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM