所以我正在尝试实现elasticsearch
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const hbs = require('hbs');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');
const fs = require('fs');
const sslRedirect = require('heroku-ssl-redirect');
const elasticsearch = require('elasticsearch');
var app = express();
// elasticsearch
var connectionString = process.env.SEARCHBOX_URL;
var client = new elasticsearch.Client({
if (process.env.NODE_ENV === 'production') {
hosts: connectionString
} else {
hosts: ['http://localhost:9200']
}
});
client.ping({
requestTimeout: 30000,
}, function(error) {
if (error) {
console.error('elasticsearch cluster is down!');
} else {
console.log('Everything is ok');
}
});
但是我收到以下错误。
if (process.env.NODE_ENV === 'production') {
^
SyntaxError: Unexpected token .
我已经看过我如何在各个地方获得环境,而且我也是这样做的。所以不确定为什么错误会在这里抛出。
答案 0 :(得分:3)
您的语法无效。如果您需要有条件使用ternary operator:
const isProduction = process.env.NODE_ENV === 'production'
const client = new elasticsearch.Client({
hosts: isProduction ? connectionString : ['http://localhost:9200']
});