简体   繁体   English

在meanjs rest api服务器中启用cors

[英]enabling cors in meanjs rest api server

I am creating an api (server side) based on 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 我正在基于Meanjs.org最新版本(0.4.0)创建一个api(服务器端),我设法仅拉出MEN部分并在http:// localhost:3000 / api中创建了一个

as the frontend part i created an Angularjs in http://localhost:4000/ 作为前端部分,我在http:// localhost:4000 /中创建了Angularjs

and then i run both application using (P)ackage (M)anager 2 然后我使用(P)ackage(M)anager 2运行两个应用程序

I am trying to create a user by sending user credentials using $resource like this 我正在尝试通过使用$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);
    });
};

While in my server side's express.js 在服务器端的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;
};

I am using node cors package to enable cors and just do app.options('*', cors()); 我正在使用节点cors包启用cors并只是执行app.options('*', cors()); to enable pre-flight across-the-board 全面启用飞行前

But when i am trying to do a POST to http://localhost:3000/api/auth/signup i can see that my user is being saved to the database just fine but it doesn't give me any response and chrome console is giving me this 但是,当我尝试对http://localhost:3000/api/auth/signup进行POST ,我可以看到我的用户正好保存到数据库中,但是它没有任何响应,Chrome控制台是给我这个

XMLHttpRequest cannot load http://localhost:3000/api/auth/signup . XMLHttpRequest无法加载http:// localhost:3000 / api / auth / signup No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:4000 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:4000 '。

What did i miss? 我错过了什么?

I think you are missing app.use before all your routes: 我认为您在所有路线之前都缺少app.use:

Only express: 仅表示:

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();
});

If you are using npm cors: 如果您使用的是npm cors:

app.use(cors());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM