简体   繁体   中英

Working on mac but not on machine - linux ubuntu - node server

I've got problem with my http server, here is code:

var http = require('http');
var url = require('url');
var fs = require('fs');

http.createServer(function (req, res) {
  var q = url.parse(req.url, true);
  var filename = "." + q.pathname;


const request = require('request');


var dpi = "half api code type 1"; 
var ipn = "half api code type 2"; 
var close = "2nd half api code ";



var ipnTable = ['table with links'];


var dpiTable = ['table with links',];





request(dpi+dpiTable[0]+close { json: true }, (err, res, body) => {
  if (err) { return console.log(err); }
  console.log(body.url);
  console.log(body.explanation);
});



  fs.readFile(filename, function(err, data) {
    if (err) {
      res.writeHead(404, {'Content-Type': 'text/html'});
      return res.end("404 Not Found");
    } 
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}).listen(8080, 'ip / hostname of my machine - idk what to chose');

It's working on mac but i'm receiving some error on machine, look at this:

0|serwer2  |     at Server.<anonymous> (/home/ubuntu/smcheck/serwer2.js:10:17)
0|serwer2  |     at emitTwo (events.js:106:13)
0|serwer2  |     at Server.emit (events.js:191:7)
0|serwer2  |     at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:546:12)
0|serwer2  |     at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
0|serwer2  | Error: Cannot find module 'request'
0|serwer2  |     at Function.Module._resolveFilename (module.js:469:15)
0|serwer2  |     at Function.Module._load (module.js:417:25)
0|serwer2  |     at Module.require (module.js:497:17)
0|serwer2  |     at require (internal/module.js:20:19)
0|serwer2  |     at Server.<anonymous> (/home/ubuntu/smcheck/serwer2.js:10:17)
0|serwer2  |     at emitTwo (events.js:106:13)
0|serwer2  |     at Server.emit (events.js:191:7)
0|serwer2  |     at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:546:12)

Please, help me guys, I don't have any idea what to do...

I'm trying to send request to the api of one website, on mac I'm receiving undefined responses - probably because the api server is using revdns for authentication.

The question is, why I've got all the time this same problem and if u have any idea how to put response from the api server into some div it would be nice.

Thanks, beer for all who's interested in.

You cannot require globally installed npm modules by default

Instead of npm install request -g do npm install request --save

It's working on your Mac probably because there is a NODE_PATH environment variable pointing to global node_modules directory. In Linux, you can do this by: export NODE_PATH=/usr/lib/node_modules (path could be different on your machine)

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