[英]NodeJS server error
My third day on learning Angular. 我学习Angular的第三天。 I reached a point of learning about making ajax calls but I'm stuck at a point where a tutorial asked me to run
server.js
. 我到达了学习有关进行Ajax调用的程度,但是我陷入了一个教程要求我运行
server.js
。 I have installed nodejs
and express
but when I try to run node server.js
我已经安装了
nodejs
并express
但是当我尝试运行node server.js
EDIT: 编辑:
I tried to changed the code, from express 3.0
to express 4.0
following the examples on their website. 我尝试按照他们网站上的示例将代码从
express 3.0
更改为express 4.0
。 My new server.js
file looks like this: 我的新
server.js
文件如下所示:
var express = require('express'),
app = express();
/* EXPRESS 3.0
app.configure(function () {
app.use(express.static(__dirname, '/'));
});
*/
// EXPRESS 4.0
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
// configure stuff here
app.use(express.static(__dirname, '/'));
}
/*EXPRESS 3.0
app.get('/customers/:id', function(req, res) {
var customerId = parseInt(req.params.id);
var data = {};
for (var i = 0, len = cutomers.length; i < len; i++) {
if (customers[i].id === customerId) {
data = customer[i];
break;
}
}
res.json(data)
});
*/
//EXPRESS 4.0
app.route('/customers/:id')
.get(function(req, res) {
var customerId = parseInt(req.params.id);
var data = {};
for (var i = 0, len = cutomers.length; i < len; i++) {
if (customers[i].id === customerId) {
data = customer[i];
break;
}
}
res.json(data)
})
/* EXPRESS 3.0
app.get('/customers', function(req, res) {
res.json(customers);
});
*/
//EXPRESS 4.0
app.route('/customers')
.get (function(req, res) {
res.json(customers);
})
app.listen(3000);
console.log('Express listening on port 3000');
var customers = [
{
id: 1,
joined: '2005-09-07',
name: 'Mayweather',
city: 'Brooklyn',
orderTotal: '43.1299',
orders: [
{
id: 1,
product: 'Pencils',
total: 9.9956
}
]
},
{
id: 2,
joined: '2005-09-07',
name: 'Jason',
city: 'Cleveland',
orderTotal: '89.8933',
orders: [
{
id: 1,
product: 'iPad',
total: 20.9956
}
]
},
{
id: 3,
joined: '1999-08-27',
name: 'Jade',
city: 'Wroclaw',
orderTotal: '77.0092',
orders: [
{
id: 1,
product: 'Pillows',
total: 12.2311
}
]
},
{
id: 4,
joined: '2015-09-01',
name: 'David',
city: 'Accra',
orderTotal: '13.8465',
orders: [
{
id: 1,
product: 'Serek',
total: 11.4782
}
]
},
{
id: 5,
joined: '2001-01-18',
name: 'Doyet',
city: 'Paris',
orderTotal: '23.9930',
orders: [
{
id: 1,
product: 'Serek',
total: 11.4782
}
]
}];
And terminal looks like this: 终端看起来像这样:
node server.js
/Users/siaw/Desktop/angularjshelloworld/node_modules/express/node_modules/serve-static/index.js:47
var opts = Object.create(options || null)
^
TypeError: Object prototype may only be an Object or null: /
at Function.create (native)
at Function.serveStatic (/Users/siaw/Desktop/angularjshelloworld/node_modules/express/node_modules/serve-static/index.js:47:21)
at Object.<anonymous> (/Users/siaw/Desktop/angularjshelloworld/server.js:15:27)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Any way I can fix this? 有什么办法可以解决这个问题?
Application.configure()在3.x中记录为旧版,在4.x中已删除 。
if you are using Express 4.0 then the configure is removed. 如果使用的是Express 4.0,则会删除配置。 See http://expressjs.com/guide/migrating-4.html#other-changes its Simple mistake.
请参阅http://expressjs.com/guide/migrating-4.html#other-更改其“简单”错误。
Have you tried installing express? 您是否尝试安装Express? Do
npm install -g express
on your console (you may need to sudo npm install -g express
if you don't have sufficient privileges), and see if the error goes away. 在控制台上执行
npm install -g express
(如果您没有足够的特权,则可能需要sudo npm install -g express
),然后查看错误是否消失。
Just needed to change to 只需更改为
app.use(express.static(__dirname + '/'));
instead of app.use(express.static(__dirname, '/'));
而不是
app.use(express.static(__dirname, '/'));
** Thanks to @stride on IRC. **感谢IRC上的@stride。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.