简单的快速应用程序'不能GET /'

我正在运行:节点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 / 

目录看起来像这样,

在这里输入图像说明

编辑:

在/ 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.'} }; 

就像你为'/api/posts'定义了一个路由一样,你需要为'/'定义一个路由。

例:

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

这是因为您尚未为您的/页面实际定义路线。

要做到这一点,你可以改变/api/posts只是/在你的例子。