为什么以下快速设置不运行localhost服务器?

时间:2017-04-11 02:06:35

标签: node.js express

这是我的快速设置。我正在使用.ejs并使用Vue.js前端框架运行index.ejs文件:

'use strict';
var express = require('express');
var request = require('request');
var timeout = require('connect-timeout');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(express.static('views'));
app.use(timeout('15s'));
app.enable('trust proxy');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.all('/:id', function (req, res) {
  const hash = req.params.id
  fetchBuildingsByHash(hash).then(({title, description, image, isBasicPlan}) => {
    const obj = {}
    if (isBasicPlan) {
      obj.title = 'iStaging LiveTour'
      obj.description = ''
      obj.image = 'https://raw.githubusercontent.com/alexcheninfo/vue-tmux-example/master/app/istaging.jpg'
    } else {
      obj.title = title || 'iStaging LiveTour'
      obj.description = description || ''
      obj.image = image || 'https://raw.githubusercontent.com/alexcheninfo/vue-tmux-example/master/app/istaging.jpg'
    }
    res.render('index.ejs', obj)
  }).catch((err) => {
    const obj = {
      title: 'notFound'
    }
    res.render('404.ejs', obj)
  })
});

function fetchBuildingsByHash (hash) {
  return new Promise((resolve, reject) => {
    const FETCH_BUILDINGS_URL = (process.env.SERVER_URL || 'https://vrbackendus.avosapps.us') + '/api/v1/sharecode?code=' + hash + '&isCache=true'

    request(FETCH_BUILDINGS_URL, function (error, response, body) {
      if (!error && response.statusCode === 200 && body) {
        const obj = JSON.parse(body)[0]
        const building = {}
        building.title = obj.name
        building.description = obj.description
        building.image = obj.previewPhoto || obj.logo
        building.isBasicPlan = obj.owner ? obj.owner.basicPlan : false
        resolve(building)
      } else {
        reject(error)
      }
    })
  })
}

app.all('*', function (req, res) {
  const obj = {
    title: 'noId'
  }
  res.render('404.ejs', obj)
});

app.use(function(req, res, next) {
  if (!res.headersSent) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  }
});

// error handlers
app.use(function(err, req, res, next) {
  if (req.timedout && req.headers.upgrade === 'websocket') {
    return;
  }

  var statusCode = err.status || 500;
  if (statusCode === 500) {
    console.error(err.stack || err);
  }
  if (req.timedout) {
    console.error('请求超时: url=%s, timeout=%d, 请确认方法执行耗时很长,或没有正确的 response 回调。', req.originalUrl, err.timeout);
  }
  res.status(statusCode);
  var error = {}
  if (app.get('env') === 'development') {
    error = err;
  }
  res.render('404', {
    message: err.message,
    error: error
  });
});

module.exports = app;

当我尝试从终端运行Express时:

User@User-PC MINGW32 /E/alex/livetourtest_azure (dev)
$ npm start

> express-test@1.0.0 start E:\alex\livetourtest_azure
> node app.js


User@User-PC MINGW32 /E/alex/livetourtest_azure (dev)

如您所见,服务器无法运行。

可能是什么原因?

1 个答案:

答案 0 :(得分:1)

您需要致电app.listen(3000),其中3000是您要使用的端口。