问题描述
livereload[tiny-lr] listening on 35729 ...C:es6serverbinwww:16app.set(’port’, port); ^TypeError: app.set is not a function at Object.<anonymous> (C:es6serverbinwww:16:5) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10) at run (bootstrap_node.js:389:7) at startup (bootstrap_node.js:149:9) at bootstrap_node.js:504:3
执行gulp是正常的,执行gulp--watch就报错下面是www文件
#!/usr/bin/env node/** * Module dependencies. */var app = require(’../app’);var debug = require(’debug’)(’server: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);}
app file
/** * Module dependencies. */var express = require(’express’);var routes = require(’./routes’);var user = require(’./routes/user’);var http = require(’http’);var path = require(’path’);var app = express();// all environmentsapp.set(’port’, process.env.PORT || 3000);app.set(’views’, path.join(__dirname, ’views’));app.set(’view engine’, ’ejs’);app.use(express.favicon());app.use(express.logger(’dev’));app.use(express.json());app.use(express.urlencoded());app.use(express.methodOverride());app.use(app.router);app.use(express.static(path.join(__dirname, ’public’)));// development onlyif (’development’ == app.get(’env’)) { app.use(express.errorHandler());}app.get(’/’, routes.index);app.get(’/users’, user.list);http.createServer(app).listen(app.get(’port’), function(){ console.log(’Express server listening on port ’ + app.get(’port’));});
看了stackoverflow回答后修改依旧报错:
events.js:160 throw er; // Unhandled ’error’ event ^Error: listen EADDRINUSE :::3000 at Object.exports._errnoException (util.js:1018:11) at exports._exceptionWithHostPort (util.js:1041:20) at Server._listen2 (net.js:1258:14) at listen (net.js:1294:10) at Server.listen (net.js:1390:5) at Object.<anonymous> (C:es6serverbinwww:28:24) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12)
问题解答
回答1:你app里面没有把app export出去,所以实际上你www文件拿到的app是undefined,自然也没有set方法。app后面加上
module.exports = app;