[英]Node.js and ExpressJS body-parser package retrieve data
OK first time user of Node, and I am trying to send data to backend/index.js
好的,第一次使用Node的用户,我正在尝试将数据发送到backend/index.js
Requirements 要求
Whats the problem 有什么问题
The post is 200 success, that works. 该职位成功200英镑,有效。
however when I navigate to : 但是,当我导航到:
http://localhost:8080/backend/index http:// localhost:8080 / backend / index
I get : 我得到:
Cannot GET /backend/index 无法获取/后端/索引
Where am I going wrong ? 我要去哪里错了?
Here is my front end post post 这是我的前端帖子
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:8080/backend/index.js",
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"id": "1223",
"token": "223",
"geo": "ee"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
I am trying to retrieve this data from and store as variable in the back end node. 我正在尝试从后端节点检索此数据并将其存储为变量。
backend/index.js 后端/ index.js
// grab the packages we need
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
// routes will go here
// start the server
app.listen(port);
console.log('Server started! At http://localhost:' + port);
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// POST http://localhost:8080/api/users
// parameters sent with
app.post('/backend/index', function(req, res) {
var user_id = req.body.id;
var token = req.body.token;
var geo = req.body.geo;
res.send(user_id + ' ' + token + ' ' + geo);
});
I think you didn't specify GET method, you can do like this 我认为您没有指定GET方法,您可以这样做
app.get('/', function(req, res) {
res.send('do something 1');
});
app.get('/backend/index', function(req, res) {
res.send('do something 2');
});
Hope this will help! 希望这会有所帮助!
Because you Post Api and you can not get result without parameter. 因为您发布Api,并且没有参数就无法获得结果。 Your Post Api will not give output until you hit only url on web browser, so need postman for Post Api. 除非您在网络浏览器上仅点击url,否则您的Post Api不会提供输出,因此需要Post Api的邮递员。
Now you get Output without any problem. 现在,您可以毫无问题地获得输出。
Thanks in Advance 提前致谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.