在 meanjs rest api 服务器中启用 cors
enabling cors in meanjs rest api server
我正在基于 Meanjs.org latest version (0.4.0) and i managed to pull off only the MEN part and create one in http://localhost:3000/api
创建一个 api(服务器端)
作为前端部分,我在 http://localhost:4000/
中创建了一个 Angularjs
然后我 运行 两个应用程序都使用 (P)ackage (M)anager 2
我正在尝试通过使用 $resource
发送用户凭据来创建用户,就像这样
angular.module('users').factory('AuthenticationResource', ['$resource',
function($resource) {
return $resource('http://localhost:3000/api/auth/signup', {}, {
post: {
method: 'POST'
}
});
}
]);
...
//In my controller
$scope.signup = function() {
AuthenticationResource.post($scope.credentials, function(response) {
$scope.authentication.user = response;
$state.go($state.previous.state.name || 'home', $state.previous.params);
});
};
而在我的服务器端 express.js
'use strict';
var config = require('../config'),
express = require('express'),
...
cors = require('cors');
...
module.exports.initModulesServerRoutes = function(app) {
// Globbing routing files
config.files.server.routes.forEach(function(routePath) {
require(path.resolve(routePath))(app);
});
};
module.exports.initCorsOption = function(app){
app.options('*', cors());
};
module.exports.init = function(db) {
// Initialize express app
var app = express();
...
// Initialise Cors options
this.initCorsOption(app);
// Initialize modules server routes
this.initModulesServerRoutes(app);
...
return app;
};
我正在使用 node cors package 来启用 cors,然后 app.options('*', cors());
来全面启用飞行前
但是当我尝试执行 POST
到 http://localhost:3000/api/auth/signup
时,我可以看到我的用户被很好地保存到数据库中,但它没有给我任何响应并且 chrome 控制台给我这个
XMLHttpRequest cannot load http://localhost:3000/api/auth/signup. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' is therefore not allowed access.
我错过了什么?
我认为你在所有路线之前都缺少 app.use:
只表达:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
如果您使用的是 npm cors:
app.use(cors());
我正在基于 Meanjs.org latest version (0.4.0) and i managed to pull off only the MEN part and create one in http://localhost:3000/api
创建一个 api(服务器端)作为前端部分,我在 http://localhost:4000/
中创建了一个 Angularjs然后我 运行 两个应用程序都使用 (P)ackage (M)anager 2
我正在尝试通过使用 $resource
发送用户凭据来创建用户,就像这样
angular.module('users').factory('AuthenticationResource', ['$resource',
function($resource) {
return $resource('http://localhost:3000/api/auth/signup', {}, {
post: {
method: 'POST'
}
});
}
]);
...
//In my controller
$scope.signup = function() {
AuthenticationResource.post($scope.credentials, function(response) {
$scope.authentication.user = response;
$state.go($state.previous.state.name || 'home', $state.previous.params);
});
};
而在我的服务器端 express.js
'use strict';
var config = require('../config'),
express = require('express'),
...
cors = require('cors');
...
module.exports.initModulesServerRoutes = function(app) {
// Globbing routing files
config.files.server.routes.forEach(function(routePath) {
require(path.resolve(routePath))(app);
});
};
module.exports.initCorsOption = function(app){
app.options('*', cors());
};
module.exports.init = function(db) {
// Initialize express app
var app = express();
...
// Initialise Cors options
this.initCorsOption(app);
// Initialize modules server routes
this.initModulesServerRoutes(app);
...
return app;
};
我正在使用 node cors package 来启用 cors,然后 app.options('*', cors());
来全面启用飞行前
但是当我尝试执行 POST
到 http://localhost:3000/api/auth/signup
时,我可以看到我的用户被很好地保存到数据库中,但它没有给我任何响应并且 chrome 控制台给我这个
XMLHttpRequest cannot load http://localhost:3000/api/auth/signup. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' is therefore not allowed access.
我错过了什么?
我认为你在所有路线之前都缺少 app.use:
只表达:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
如果您使用的是 npm cors:
app.use(cors());