我對這個節點js的東西有點新。所以,請忍受我。節點JS:500服務器錯誤
我在dotNetFin.js中有一個json字符串數組。我不確定爲什麼會出現錯誤(當我切換到關於頁面),並且在命令提示符窗口中顯示「未定義不是函數」
此外,如果我包括在app.js中的json數組。
那麼這裏是我的代碼:
app.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
var dotnetTeam = ["V",
"M",
"A",
"H",
"A",
"G",
"K"];
var getTeamData = function() {
return dotnetTeam;
};
個main.handlebars
<!doctype html>
<html>
<head>
<h2>Fin</h2>
</head>
<body>
{{{body}}}
</body>
</html>
about.handlebars
<h1>About Fin</h1>
<h2>Dotnet Team</h2>
<h3>{{dotnet}}</h3>
錯誤快照:
在dotnetFin.js中,是否通過'module.exports.getTeamData = getTeamData'輸出函數? –