Edward Edward - 1 year ago 124
Node.js Question

Different result in Node.js command and VS2015

I made a test with below node.js code in Node.js command and VS2015.

var fs = require('fs');
var http = require('http');
var url = require('url');
var ROOT_DIR = "Scripts/http/";
http.createServer(function (req, res) {
var urlObj = url.parse(req.url, true, false);
var reqPath = ROOT_DIR + urlObj.pathname;
fs.readFile(ROOT_DIR + urlObj.pathname, function (err, data) {

if (err) {
console.log('http server is open');

If I run this from VS2015, and then I enter "http://localhost:1111/hello.html", it output the content in hello.html.
But if I open run below command, it tells me 'http server is open', but if I enter above url, it tells me file not found.

C:\Users\xx>cd E:\xx\NodeJs
C:\Users\xx>node E:\xx\NodeJsProject\Scripts\http\http_server_static.js

I assume it is relative path issue, but I do not know details about it.

Thanks for your help.

Answer Source

Making my comment into an answer so you can wrap up this question...

I'd suggest you probably have a problem with the relative path where the module directory is different in your two environments. Change it to an absolute path and it should perform the same in both places. You can call path.resolve() on your relative path and see what it comes out to be in both cases. Probably different.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download