No 'Access-Control-Allow-Origin' - Node / Apache Port Issue

后端 未结 13 1692
有刺的猬
有刺的猬 2020-11-22 02:27

i\'ve created a small API using Node/Express and trying to pull data using Angularjs but as my html page is running under apache on localhost:8888 and node API is listen on

13条回答
  •  失恋的感觉
    2020-11-22 03:12

    /**
     * Allow cross origin to access our /public directory from any site.
     * Make sure this header option is defined before defining of static path to /public directory
     */
    expressApp.use('/public',function(req, res, next) {
        res.setHeader("Access-Control-Allow-Origin", "*");
        // Request headers you wish to allow
        res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        // Set to true if you need the website to include cookies in the requests sent
        res.setHeader('Access-Control-Allow-Credentials', true);
        // Pass to next layer of middleware
        next();
    });
    
    
    /**
     * Server is about set up. Now track for css/js/images request from the 
     * browser directly. Send static resources from "./public" directory. 
     */
    expressApp.use('/public', express.static(path.join(__dirname, 'public')));
    If you want to set Access-Control-Allow-Origin to a specific static directory you can set the following.

提交回复
热议问题