简体   繁体   English

在 mac 上工作但不在机器上工作 - linux ubuntu - 节点服务器

[英]Working on mac but not on machine - linux ubuntu - node server

I've got problem with my http server, here is code:我的 http 服务器有问题,这是代码:

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:它在 mac 上工作,但我在机器上收到一些错误,看看这个:

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.我正在尝试向一个网站的 api 发送请求,在 mac 上我收到未定义的响应 - 可能是因为 api 服务器正在使用 revdns 进行身份验证。

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.问题是,为什么我总是遇到同样的问题,如果您知道如何将来自 api 服务器的响应放入某个 div 中,那就太好了。

Thanks, beer for all who's interested in.谢谢,啤酒给所有感兴趣的人。

You cannot require globally installed npm modules by default默认情况下,您不能要求全局安装 npm 模块

Instead of npm install request -g do npm install request --save而不是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.它在您的 Mac 上运行可能是因为有一个指向全局node_modules目录的 NODE_PATH 环境变量。 In Linux, you can do this by: export NODE_PATH=/usr/lib/node_modules (path could be different on your machine)在 Linux 中,您可以通过以下方式执行此操作: export NODE_PATH=/usr/lib/node_modules (您机器上的路径可能不同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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