简体   繁体   English

在哪里使用 nodejs/npm start 查看 console.log 输出?

[英]Where to view console.log output using nodejs/npm start?

very new to react/node/using npm start to start a server. react/node/使用 npm start 来启动服务器非常新。 Using npm start to start the server works and displays my react code, but I need to do some debugging.使用 npm start 启动服务器工作并显示我的反应代码,但我需要做一些调试。 However, my console.logs aren't outputting to the browser console.但是,我的 console.logs 没有输出到浏览器控制台。 After that, I thought to check my terminal (which i think is where node console.log outputs to), but since I used npm start, the terminal that I launched the application from is stuck on this screen:在那之后,我想检查我的终端(我认为这是 node console.log 输出到的地方),但由于我使用了 npm start,我启动应用程序的终端卡在这个屏幕上:


Compiled successfully!

You can now view my-app in the browser.

  Local:            http://localhost:3000/
  On Your Network:  http://[ip_address_here]:3000/

Note that the development build is not optimized.
To create a production build, use npm run build.

And therefore, I cannot read any console output on this screen.因此,我无法在此屏幕上读取任何控制台输出。 I feel like this should be a super basic fix and I'm probably just overlooking something extremely obvious, but could somebody help me out here?我觉得这应该是一个超级基本的修复,我可能只是忽略了一些非常明显的东西,但是有人可以帮我吗? Thanks.谢谢。

EDIT: I was asked to share some code, here it is:编辑:我被要求分享一些代码,这里是:

Here is a snippet of my App.js file:这是我的 App.js 文件的一个片段:


import React from 'react';

//quick class
class App extends React.Component {
    constructor(props){
        super(props);

        this.state = {
            testRows : null
        }

        this.getTest = this.getTest.bind(this);
    }

    //get some rows from my api using a local database with sqlite3
    async getTest(){
        try{
            console.log("hello?");
            const response = await fetch('http://localhost:3000/api/test');
            if(response.ok){
                const JSONresponse = response.json();
                console.log(JSOnresponse);
                this.setState({testRows : JSONresponse});
            }
            else{
                this.setState({testRows: "test error"});
                console.log('There was an error with the fetch');
            }
        }
        catch(error){
            console.log(error);
        }
    }

    //render some text, a button, and the result
    render(){
        let testResults;
        if(this.state.testRows){
            testResults = Object.keys(this.state.testRows).map((data) => {
                    return <h1>{data.name}</h1>
                }
            )
        }
        else{
            testResults = "null";
        }


        return(
            <div>
                <h2>Some testing going on here</h2>
                <button onClick={this.getTest}>
                    Press me for test!
                </button>
                <h3>{testResults}</h3>
            </div>
        );
    }
}

export default App;

Here is a snippet of my api.js file:这是我的 api.js 文件的片段:


apiRouter.get('/test', (req, res, next) => {
    db.get('SELECT * FROM Test', (error, rows) => {
        if(error){
            console.log(error);
        }
        else{
            console.log("got the test");
            console.log(rows);
            res.send(rows);
        }
    })
})

The router is mounted and everything else in other parts of the code, but the console.logs there don't show up anywhere (dev tools console or terminal) either.路由器已安装,其他所有内容都在代码的其他部分,但那里的 console.logs 也没有出现在任何地方(开发工具控制台或终端)。

From what I understood you have two pieces of code running in different places.据我了解,您有两段代码在不同的地方运行。 Both should be served in order to execute the code.两者都应该被提供以执行代码。

The App.js file seems to be a React App (frontend) that should run on the browser. App.js文件似乎是一个应该在浏览器上运行的 React 应用程序(前端)。 To see the console messages you should:要查看控制台消息,您应该:

  • Open the terminal and serve the App: npm start打开终端并提供应用程序: npm start
  • Open the browser打开浏览器
  • Visit the URL indicated访问指定的 URL
  • Open the Developer Tools (usually F12)打开开发者工具(通常是 F12)

Also, try to place the log right after the class declaration or somewhere else to isolate possible other problems.此外,尝试将日志放在类声明之后或其他地方以隔离可能的其他问题。

The api.js on the other hand is a backend code, so the console messages should be logged in the terminal where it is running.另一方面, api.js是后端代码,因此控制台消息应记录在运行它的终端中。 Assuming you have the complete file api.js and not just that snippet, to run the server and see the logs you should:假设您拥有完整的文件api.js而不仅仅是该片段,要运行服务器并查看日志,您应该:

  • On the terminal and serve the API: node api.js在终端上并提供 API: node api.js
  • Open the browser打开浏览器
  • Visit the URL indicated访问指定的 URL
  • The message will show up on the terminal该消息将显示在终端上

I will do the same recommendation, if you place the log command at the beginning of the script the message will be shown as the server start, without the need to go to the browser.我将执行相同的建议,如果您将 log 命令放在脚本的开头,则消息将显示为服务器启动,而无需转到浏览器。 Isolating problems is a good way to tackle them.隔离问题是解决问题的好方法。

If you don't have the full api.js , try a simple snippet: Getting Started .如果您没有完整的api.js ,请尝试一个简单的片段: Getting Started Just add the console.log('Console says hi!') at the beginning, or after the "Hello World" line if you want to see it after opening the browser.如果您想在打开浏览器后看到它,只需在开头添加console.log('Console says hi!')或在“Hello World”行之后添加。

A final remark would be to make sure front and back are not being served on the same port (3000).最后一点是确保前端和后端不在同一个端口 (3000) 上提供服务。

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

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