gpt4 book ai didi

node.js - 简单 express 应用程序 'Cannot GET/'

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:40 24 4
gpt4 key购买 nike

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

我运行了《CORS in Action》一书中的以下内容 - 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/posts 中我有

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!')
})

关于node.js - 简单 express 应用程序 'Cannot GET/',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41474343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com