简体   繁体   English

为什么我的节点应用程序不登录到终端或Chrome控制台?

[英]Why isn't my node app logging to the terminal or the Chrome console?

I'm a noob when it comes to back-end with Node. 对于Node的后端,我是一个菜鸟。

I'm running this file on an Ubuntu 14 server in the cloud. 我正在云中的Ubuntu 14服务器上运行此文件。 The file is being run with pm2. 该文件正在使用pm2运行。 (ex. pm2 start newServer). (例如,pm2启动newServer)。 The outputs from the console.log()s aren't appearing either in the terminal I've ssh'd into or in the javascript console in the browser. console.log()的输出既不会出现在我所连接的终端中,也不会出现在浏览器的JavaScript控制台中。 How can I get the file to log output into the terminal I've ssh'd into? 如何获取文件以将输出记录到我已登录的终端中? Or am I going about this the wrong way entirely (do I need to start learning unit testing, is there some logging module I should be using, ...?) 还是我完全以错误的方式解决了这个问题(我是否需要开始学习单元测试,是否应该使用某些日志记录模块,...?)

EDIT: The pm2 logs (viewed with 'pm2 logs' or 'pm2 logs [app name]) don't contain any of the output from the console.log statements in the node script. 编辑:pm2日志(使用“ pm2日志”或“ pm2日志[应用程序名称]查看”)不包含节点脚本中console.log语句的任何输出。

EDIT2: I also don't get log output anywhere when I stop the pm2 process and just run the file with node. EDIT2:当我停止pm2进程并仅使用node运行文件时,我也不会在任何地方获得日志输出。

与节点一起运行的结果

EDIT3: Still no output when I run the script with node on my local machine and access the page in the browser via localhost:8080. EDIT3:当我在本地计算机上运行带有节点的脚本并通过localhost:8080访问浏览器中的页面时,仍然没有输出。

FINAL EDIT: Figured it out. 最终编辑:弄清楚了。 It's because my gulpfile had strip-debug in it. 这是因为我的gulpfile中包含了带调试功能。 Being new to build systems, I didn't realize that said option removed console.logs. 作为构建系统的新手,我没有意识到该选项删除了console.logs。 I found out by using node-inspector to actually look at the node code that was being run, and finding that my console.log statements had been replaced with "void 0"s. 我发现通过使用node-inspector来实际查看正在运行的节点代码,并发现我的console.log语句已被替换为“ void 0”。

var express = require('express'); //Require express for middleware use
const app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http); //IO is the server

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');

//BASICS
app.use(express.static(__dirname + "/served")); //Serve static files

app.get('/', function(req, res){
  res.sendFile('./index.html');

});

io.on('connection', function(socket){
  // socket.on('chat message', function(msg){
    // io.emit('chat message', msg);
  // });
  console.log("A user connected: ", socket.id);
});

http.listen(8080, function(){
  console.log('listening on *:8080');
});

console.log('test')

// MONGODB
var url = 'mongodb://localhost:27017/test';
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server.");
  db.close();
});

I'm not an expert on pm2, but from a quick glance it's designed for production use, and won't log anything to the terminal. 我不是pm2方面的专家,但是从一眼看,它是为生产使用而设计的,不会在终端上记录任何内容。

Looking at the page 看页面

https://github.com/Unitech/pm2 https://github.com/Unitech/pm2

There is a section on Log facilities which tells you how to look at the logs, with a command like this 有一个关于日志功能的部分,它告诉您如何使用这样的命令查看日志

pm2 logs APP-NAME       # Display APP-NAME logs

Otherwise if you really just want to see what it is doing, just run your code with node, eg 否则,如果您真的只想看看它在做什么,则只需使用node运行代码,例如

node myprogram.js

When using the pm2 module the logs are saved per separate node. 使用pm2模块时,日志按每个单独的节点保存。 In order to view them you can issue the command pm2 logs to view the logs for each node. 为了查看它们,您可以发出命令pm2 logs来查看每个节点的日志。

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

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