我们如何在明确的应用程序上应用CORS

时间:2016-12-06 18:28:45

标签: javascript mysql angularjs node.js express

Hello 我创建了一个简单的nodejs + express + mysql + angular + yoeman应用程序。

在该应用程序中,我执行以下操作:
1。我创建了快速应用程序并在端口上运行服务器:3000
2. 我创建了一个简单的rest api,从mysql获取产品(我有复杂的表)db。
这是从 productsModel.js

获取行的 productsController.js
var productModel = require('../models/productsModel');

var products = {};

/*controller that handles Products CRUD*/
products.list = function(req, res){
  console.log('REQUEST TO GET PRODUCTS (productsController)')
  var prod = productModel.list();
  prod.then(function(products){
    res.send(products);
  }, function(){
    res.send({status:'error',error:'Error occured while fetching data from database.'});
  });
}

module.exports = products;

3. 对于客户端,我运行 gulp serve 并在 localhost:4000 /#/

上打开角度应用程序

4. 如果设置了其余api服务器的基本网址,请在 index.config.js 内: function config($ logProvider,toastrConfig,RestangularProvider){

RestangularProvider.setBaseUrl('http://localhost:3000');

5. 在主控制器内部,我调用 restangular 来获取数据,如下所示:

  function MainController($timeout, $scope, $http, $log, Restangular) {
    var vm = this;

    vm.awesomeThings = [];
    vm.classAnimation = '';
    vm.creationDate = 1480625671827;

    Restangular.all("products").getList().then(function() {
      console.log("All ok");
    }, function(response) {
      console.log("Error with status code", response.status);
    });

6。两个2000字的截图(节点服务器和客户端错误)。

node server gets data from mysql , correctly

The errors that i get on the client

我错过了什么吗?任何帮助将不胜感激。

更新问题(显示我的app.js)当我使用标题我仍然得到错误

var express = require('express');
var cors    = require('cors');/*add cors module to support CORS*/
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var         _  =  require('lodash');
var routes     =  require('./routes/routes');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
/*app.use(cors());*/
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json.
app.use(bodyParser.json());
app.use(function(req, res, next) {
   res.header("Access-Control-Allow-Origin", "*");
   res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
   next();
 });


app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'app')));/*έκανα το folder 'public' -> 'app' */

app.use('/', index);
app.use('/users', users);

/*Load the routes*/
routes(app); /*here i have my /products route*/

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

0 个答案:

没有答案