node.js 节点 JS:500 服务器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33234641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:23:51  来源:igfitidea点击:

Node JS: 500 Server Error

node.js

提问by Ajay

I am kinda new to this node js thing. So, please bear with me.

我对这个 node js 东西有点陌生。所以,请耐心等待。

I have got a json string array in dotNetFin.js. I am not sure as to why error comes up(when i switch over to About page), and, in the command prompt window, it shows up "Undefined is not a function"

我在 dotNetFin.js 中有一个 json 字符串数组。我不确定为什么会出现错误(当我切换到关于页面时),并且在命令提示符窗口中,它显示“未定义不是函数”

Also, everything works fine if I include that json array in app.js.

此外,如果我在 app.js 中包含该 json 数组,一切正常。

Well here is my code:

好吧,这是我的代码:

app.js

应用程序.js

var express = require('express');

var app = express();
app.set('port', process.env.PORT || 3000);

app.get('/', function (req, res) {
    res.render('home');
});

var teamData = require('./dotnetFin.js');
app.get('/about', function (req, res) {
    res.render('about', { dotnet: teamData.getTeamData() });
});
//custom 404 page
app.use(function (req, res) {
    res.type('text/plain');
    res.status(404);
    res.send('404 Not Found');
});

app.use(function (err, req, res, next) {
    console.log(err.stack);
    res.type('text/plain');
    res.status(500);
    res.send('500 Server Error');
});

app.listen(app.get('port'), function () {
    console.log('Express started on server' + app.get('port'));
});

var handleBars = require('express3-handlebars').create({ defaultLayout: 'main' });
app.engine('handlebars', handleBars.engine);
app.set('view engine', 'handlebars');

dotnetFin.js

dotnetFin.js

var dotnetTeam = ["V",  
                        "M", 
                        "A",  
                        "H", 
                        "A", 
                        "G", 
                        "K"];

var getTeamData = function () {
    return dotnetTeam;
};

main.handlebars

main.handlebars

<!doctype html>
<html>
  <head>
    <h2>Fin</h2>
  </head>
  <body>
    {{{body}}}
  </body>
</html>

about.handlebars

about.handlebars

<h1>About Fin</h1>
<h2>Dotnet Team</h2>
<h3>{{dotnet}}</h3> 

Error Snapshot:

错误快照:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

采纳答案by User97

You need to export your getTeamDatafunction from dotnetFin.js file. So just change,

您需要getTeamData从 dotnetFin.js 文件中导出您的函数。所以只要改变,

var getTeamData = function () {
    return dotnetTeam;
};

with

exports.getTeamData = function () {
    return dotnetTeam;
};

and your API should work.

并且您的 API 应该可以工作。

You always need to export variables or methods from your node module (.js file) in order to be able to access them from some other file which requires that module.

您总是需要从节点模块(.js 文件)中导出变量或方法,以便能够从需要该模块的其他文件中访问它们。

回答by LionC

requiredoes not simply include the file, like for example includein C/C++. There is an own API for modulesthat can then be required, however your case can be solved a lot easier by just changing your dotnetFin.jsfile to a pure json file:

require不简单地包含文件,例如include在 C/C++ 中。有一个自己的模块 API,然后可以required,但是只需将dotnetFin.js文件更改为纯 json 文件,就可以更轻松地解决您的问题:

dotnetFin.json:

dotnetFin.json

["V",  
"M", 
"A",  
"H", 
"A", 
"G", 
"K"]

and then just requiring that file:

然后只需要该文件:

var dotnetFin = require('./dotnetFin.json')

Now dotnetFiscontains the array defined in the file.

现在dotnetFis包含文件中定义的数组。