为什么我的rest端点不能在我的节点服务器上工作

Why do my rest endpoints not work on my node server?

本文关键字:我的 服务器 工作 节点 不能 rest 端点 为什么      更新时间:2023-09-26

我的index.js文件是这样的:

var http = require('http');
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser')
var app = express();
var currentVideo = 'https://www.youtube.com/embed/9Q-ovNuireY';
// parse application/json
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(res) {
  console.log("sup");
});
app.get('/currentVideo', function(req, res) {
  res.send(200, {youtubeSrc: currentVideo});
});
http.createServer(app).listen(process.env.PORT ||3000, function() {
  console.log('Example app listening on port 3000!');
});

我的静态页面在公共加载,我的服务器启动良好,打印出示例应用程序日志。当我尝试点击get端点时,它不起作用。我一直在点击http://localhost:{port}/currentVideo,我得到一个404

我复制了您的代码并使用curl在本地进行了测试。这个请求工作得很好。

你能分享你的公共页面吗?当您尝试加载数据时,在Chrome开发人员工具控制台注意到任何日志?

curl -v http://localhost:3000/currentVideo
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /currentVideo HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 58
< ETag: W/"3a-FMsS5hMXa1tcIFnGeYHinQ"
< Date: Sun, 18 Sep 2016 20:34:04 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
{"youtubeSrc":"https://www.youtube.com/embed/9Q-ovNuireY"}