AWS Route 53 NodeJS Express子域

时间:2018-03-28 00:38:20

标签: node.js express subdomain amazon-route53

我们在Node JS中创建了一个应用程序,我们创建了一个LoadBalancer,我们已经使用Express和Forever运行应用程序,一切正常,当我们请求LoadBalancer的URL时,我们得到了响应。

{
    "status": "success",
    "message": "Parcel Pending API",
    "data": {}
}

使用Code 200。

但是当我们在Route53上创建一个子域" subdomain.domain.com",并在LoadBalancer上指定一个别名时,子域不起作用。

我们的app.js:

require('./config/config');     //instantiate configuration variables
require('./global_functions');  //instantiate global functions

console.log("Environment:", CONFIG.app)

const express       = require('express');
const logger        = require('morgan');
const bodyParser    = require('body-parser');
const passport      = require('passport');
const subdomain = require('express-subdomain');
const v1 = require('./routes/v1');
const vhost = require('vhost');
const app = express();

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')));

//Passport
app.use(passport.initialize());

//DATABASE
const models = require("./models");
models.sequelize.authenticate().then(() => {
    console.log('Connected to SQL database:', CONFIG.db_name);
})
.catch(err => {
    console.error('Unable to connect to SQL database:',CONFIG.db_name, err);
});
if(CONFIG.app==='dev'){
    models.sequelize.sync();//creates table if they do not already exist
    // models.sequelize.sync({ force: true });//deletes all tables then recreates them useful for testing and development purposes
}
// CORS
app.use(function (req, res, next) {
    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', '*');
    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization, Content-Type');
    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);
    // Pass to next layer of middleware
    next();
});

app.use('/v1', v1);
app.use(subdomain('api', v1));

app.use('/', function(req, res){
    res.statusCode = 200;//send the appropriate status code
    res.json({status:"success", message:"Parcel Pending API", data:{}})
});

// 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 个答案:

没有答案