javascript - app.set('port', port) 'TypeError: undefined is not a function'. Beginner, need ideas -
i'm amateur learning build node.js. i've been following tutorial create first node.js app. worked until entered 'npm start'. log is:
c:\node\nodeteest3\bin\www:16 typeerror: undefined not function @ object.<anonymous> m+<c;\node\nodetest3\bin\www:16:5 @ module_compile (module.js:460:26) @ object.module._extensions..js (module.js:478:10) @ module.load (module.js:355:32) @ function.module._load (module.js:310:12) @ function.module.runmain (module.js:501:10) @ startup(node.js:129:16) @ node.js:814:3
then output 20 lines starting "npm err! " + filepaths, don't think necessary, error seems in bin file. code is
#!/usr/bin/env node /** * module dependencies. */ var app = require('../app'); var debug = require('debug')('nodetest3:server'); var http = require('http'); /** * port environment , store in express. */ var port = normalizeport(process.env.port || '3000');
this error points to:
[app.set('port', port);]
-------^error pointer @ 's'-so set------------
app.set('port', port); /** * create http server. */ var server = http.createserver(app); /** * listen on provided port, on network interfaces. */ server.listen(port); server.on('error', onerror); server.on('listening', onlistening); /** * normalize port 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 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 friendly messages switch (error.code) { case 'eacces': console.error(bind + ' requires elevated privileges'); process.exit(1); break; case 'eaddrinuse': console.error(bind + ' in use'); process.exit(1); break; default: throw error; } } /** * event listener http server "listening" event. */ function onlistening() { var addr = server.address(); var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; debug('listening on ' + bind); } said in beginning, i'm complete beginner command-line/github, i'm in love it. try practice every night after finish homework, , getting frustrated getting stuck because haven't been able move forward 4 days now. also, i'm running on node.js , os windows 8. helps! let me know if want me post of other code; omitted not add more necessary. "../app (app.js file) added"*************************** ../app file: [ app.js ] var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); /// catch 404 , forwarding error handler app.use(function(req, res, next) { var err = new error('not found'); err.status = 404; next(err); }); var app = express(); // error handlers // development error handler // print stacktrace if (app.get('env') === 'development') { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // uncomment after placing favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyparser.json({estended: true})); app.use(bodyparser.urlencoded({extended: true})); app.use(cookieparser({extended:true})); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); app.use('/users', users); // production error handler // no stacktraces leaked user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); });
you not exporting in app.js
file. @ end of app.js
file, include following line.
module.exports = app;
see whether problem goes away.
and 1 more addition: have var app = express();
twice in app.js
.
Comments
Post a Comment