简单的快递应用程序'无法获取/'

时间:2017-01-04 22:32:26

标签: node.js express

我正在运行:node v6.2.2,npm v3.9.5,express v4.13.4

我从“行动中的CORS”一书中运行了以下内容 - Monsur Hossain。

$ node app.js

var express = require('express');
var POSTS = {
'1': {'post': 'This is the first blog post.'},
'2': {'post': 'This is the second blog post.'},
'3': {'post': 'This is the third blog post.'}
};
var SERVER_PORT = 9999;
var serverapp = express();
serverapp.use(express.static(__dirname));
serverapp.get('/api/posts', function(req, res) {
res.json(POSTS);
});
serverapp.listen(SERVER_PORT, function() {
console.log('Started server at http://127.0.0.1:' + SERVER_PORT);
});

在localhost:9999的浏览器中,我得到了,

Cannot GET /

目录如下所示,

enter image description here

编辑:

在/ api /帖子中我有

var POSTS = {
'1': {'post': 'This is the first blog post.'},
'2': {'post': 'This is the second blog post.'},
'3': {'post': 'This is the third blog post.'}
};

2 个答案:

答案 0 :(得分:2)

就像您为'/api/posts'定义路线一样,您需要为'/'定义路线。

示例:

serverapp.get('/', function (req, res) {
  res.send('Hello World!')
})

答案 1 :(得分:1)

这是因为您实际上没有为/页面定义路线。

要执行此操作,您可以在示例中将/api/posts更改为/