我首先自己创建了一个使用 Node/Express 的 API,以从一种“幼稚”的方式学习大多数程序员的做法。它运行良好,我决定尝试 express-generator。
设置好一切后,应用程序运行正常。
我添加了一堆代码(主要是在 app.js 中并导入了一些尝试的路线),我没有更改设置 express 的 bin/www 中的任何内容。
但是在启动时,我收到了指向这个特别文件 bin/www 的错误:
app.set('port', port);
^
TypeError: app.set is not a function
我不知道为什么这部分自动生成的代码现在不想工作。
这里有 2 个主要文件
bin/www : (生成-未修改)
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('API:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
应用程序.js:
// Module dependencies
const express = require('express');
const cluster = require('express-cluster'); // fork the service on different thread
const helmet = require('helmet'); // Secure HTTP header
const cors = require('cors');
const path = require('path');
const bodyParser = require('body-parser'); // Help to easily parse the body of req/res
const port = process.env.PORT || 3000;
const mongoose = require('mongoose'); // Manage MongoDB request
cluster(function(worker) {
var app = express();
// MongoDB config
const config = require('./misc/config/index'); // Config variable like MongoDB credential
mongoose.Promise = global.Promise;
mongoose.connect(config.getDBConnectionString()); // Config to cnx to mongodb
// mongoose.connect(config.getDBConnectionString(), { config: { autoIndex: false } });
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(helmet.frameguard()); // Default Value - Help to secure request by putting some setting in the header
app.use(cors()); // Handling Cross Origin Ressource Sharing
// Logfile
const log = require('./misc/log/log');
app.use(log);
// Config Landingpage to /
app.use('/assets', express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
// Entry point
const entryPoint = require('./routes/entryPoint');
app.get('/', entryPoint.index);
app.get('/api', function (req, res) {
res.redirect(301, '/');
})
// API Key handler
const auth = require('./misc/auth/auth');
app.use(auth);
// List
const list = require('./routes/list/listRouter'); // Get List endpoints
// app.use('/api/list', list);
// Map
const map = require('./routes/map/mapRouter'); // Get List endpoints
// app.use('/api/map', map);
module.exports = app;
}, {count: 2})
我导出包含 express() 的应用程序。所以我在这里缺少一些东西,但我不明白是什么。