无法运行Express.js服务器

时间:2015-06-17 10:19:33

标签: node.js express

我在运行以下代码时遇到错误。我已经附加了错误跟踪以及下面的express.js服务器代码:

app.configure(function() {
    ^
TypeError: undefined is not a function
    at Object.<anonymous> (C:\node\dashboard\server\index.js:17:5)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

代码:

'use strict';

var express = require("express"),
  contacts = require("./db_interactions"),
  version = require('./package.json').version,
  program = require('commander'),
  app = express();

/* cli */
program
  .version(version)
  .option('-p, --port <n>', 'port number', parseInt)
  .parse(process.argv);

var port = program.port || 4000;

app.configure(function() {
  app.use(express.bodyParser());
});

app.use(function(req, res, next) {
  /* Website you wish to allow to connect */
  res.setHeader('Access-Control-Allow-Origin', req.headers.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');

  /* 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 */
  if ('OPTIONS' === req.method) {
    res.send(200);
  } else {
    next();
  }
});

app.get("/:appName/users", contacts.index);
app.delete('/:appName/deletereleases/:id', contacts.deleteReleases);

app.listen(port, "127.0.0.1");

1 个答案:

答案 0 :(得分:1)

您使用的是哪个版本的快递?在express4.x中,删除了app.configure()方法。 请咨询this github wiki,以便从express3.x迁移到express4.x。