gpt4 book ai didi

angularjs - 从 Nodejs 向 Nodejs 发送请求

转载 作者:搜寻专家 更新时间:2023-10-31 23:26:32 28 4
gpt4 key购买 nike

我有一个 nodejs-express 服务器 (1) 与 mongodb 通信,还有一个 web 服务器 (2) 在 nodejs-express 和 Angularjs 中。我正在尝试发出 post 请求 1->2,但我收到 405 Method Not Allowed(尝试使用 postman ),如果我检查标题选项卡,它会显示:Allow →GET, HEAD , 选项

app.js - 来自 1 的 post 方法:

function sendToDashboard(jsonData) {
console.log ("I am here in node: ", jsonData);

request({
uri: "http://******.com:9000/getQueryJson",
method: "POST",
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
console.log(body);
});
}

server/app.js - 从 2 获取方法:

var bodyParser = require("body-parser"); // Require Body parser module
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var express = require('express');
var path = require('path');
var app = require('express')(); // Require Express module
var http = require('http').Server(app); // Http server
var bodyParser = require("body-parser"); // Require Body parser module

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.all('*', function(req, res,next) {


/**
* Response settings
* @type {Object}
*/
var responseSettings = {
"AccessControlAllowOrigin": req.headers.origin,
"AccessControlAllowHeaders": "Content-Type,X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name",
"AccessControlAllowMethods": "POST, GET, PUT, DELETE, OPTIONS",
"AccessControlAllowCredentials": true
};

/**
* Headers
*/
res.header("Access-Control-Allow-Credentials", responseSettings.AccessControlAllowCredentials);
res.header("Access-Control-Allow-Origin", responseSettings.AccessControlAllowOrigin);
res.header("Access-Control-Allow-Headers", (req.headers['access-control-request-headers']) ? req.headers['access-control-request-headers'] : "x-requested-with");
res.header("Access-Control-Allow-Methods", (req.headers['access-control-request-method']) ? req.headers['access-control-request-method'] : responseSettings.AccessControlAllowMethods);

if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}


});

app.post('/getQueryJson', function(request, response) {
console.log ("I got here");
if(response.statusCode == 200) {

console.log("TESTING......")
console.log("This is your request: ", request.body);

console.log("This is your request: ", JSON.stringify(request.body))
response.send("Query Received");
}else{
response.send(" Error code: " + response.statusCode);
}
});


http.listen(9000,function(){
console.log("Connected & Listen to port 9000");
});

文件结构:

(服务器 1)

enter image description here

(服务器 2)

enter image description here

知道为什么它找不到我的方法吗?任何帮助将不胜感激!

最佳答案

如果有人遇到类似问题,我解决了我的问题。错误出在我使用 gulp 运行/编译 Web 服务器的方式上。以下方法适用于我的情况:

   gulp.task('connect', ['styles'], function() {
var serveStatic = require('serve-static');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var methodOverride = require('method-override');


var port = process.env.PORT || 9000;

// added this part ==================================================
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded
app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT
// ==================================================

app.use(serveStatic('.tmp'));
app.use(serveStatic('app'));
app.use(express.static(__dirname + 'public')); // set the static files location /public/img will be /img for users
app.use('/bower_components', serveStatic('bower_components'));

// and this one ==================================================
require('./server/app')(app); // pass our application into our routes
// ===============================================


app.listen(port);
console.log('Magic happens on port ' + port); // shoutout to the user
exports = module.exports = app; // expose app

});

关于angularjs - 从 Nodejs 向 Nodejs 发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41407145/

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