Last active
October 27, 2015 08:17
-
-
Save ikouchiha47/df4a3f828b90c29cff46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var path = require('path'); | |
var favicon = require('serve-favicon'); | |
var logger = require('morgan'); | |
var cookieParser = require('cookie-parser'); | |
var session = require('express-session'); | |
var bodyParser = require('body-parser'); | |
var mongoose = require('mongoose'); | |
var routes = require('./routes/index'); | |
var users = require('./routes/users'); | |
var blogs = require('./routes/blogs'); | |
var options = { | |
db: { native_parser: true }, | |
server: { poolSize: 5 }, | |
replset: { rs_name: 'tweeter' } | |
}; | |
mongoose.connect('mongodb://localhost/fileupload', options, function(err, c) { | |
console.log(err, "error"); | |
}); | |
var app = express(); | |
// view engine setup | |
app.set('views', path.join(__dirname, 'views')); | |
app.set('view engine', 'jade'); | |
// uncomment after placing your favicon in /public | |
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | |
app.use(logger('dev')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(cookieParser()); | |
app.use(session({ resave: false, saveUninitialized: true, secret: 'blablablabla', cookie: { maxAge: 86400 } })); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
app.use('/', routes); | |
app.use('/users', users); | |
app.use('/blogs', blogs); | |
// catch 404 and forward to error handler | |
app.use(function(req, res, next) { | |
var err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); | |
// error handlers | |
// development error handler | |
// will 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 | |
}); | |
}); | |
} | |
// production error handler | |
// no stacktraces leaked to user | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: {} | |
}); | |
}); | |
module.exports = app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> [email protected] start /home/crotona/dev/javascript/nodejz/loadtest | |
> nodemon ./bin/www | |
27 Oct 12:50:43 - [nodemon] v1.3.7 | |
27 Oct 12:50:43 - [nodemon] to restart at any time, enter `rs` | |
27 Oct 12:50:43 - [nodemon] watching: *.* | |
27 Oct 12:50:43 - [nodemon] starting `node ./bin/www` | |
my spawn | |
{ '0': 'node', | |
'1': [ './bin/server' ], | |
'2': { env: { PORT: '3000' } } } | |
my spawn | |
{ '0': 'node', | |
'1': [ './bin/server' ], | |
'2': { env: { PORT: '3001' } } } | |
my spawn | |
{ '0': 'node', | |
'1': [ './bin/server' ], | |
'2': { env: { PORT: '3001' } } } | |
my spawn | |
{ '0': 'node', | |
'1': [ './bin/server' ], | |
'2': { env: { PORT: '3002' } } } | |
my spawn | |
{ '0': 'node', | |
'1': [ './bin/server' ], | |
'2': { env: { PORT: '3002' } } } | |
my spawn | |
{ '0': 'node', | |
'1': [ './bin/server' ], | |
'2': { env: { PORT: '3002' } } } | |
my spawn | |
{ '0': 'node', | |
'1': [ './bin/server' ], | |
'2': { env: { PORT: '3003' } } } | |
my spawn | |
{ '0': 'node', | |
'1': [ './bin/server' ], | |
'2': { env: { PORT: '3003' } } } | |
my spawn | |
{ '0': 'node', | |
'1': [ './bin/server' ], | |
'2': { env: { PORT: '3003' } } } | |
my spawn | |
{ '0': 'node', | |
'1': [ './bin/server' ], | |
'2': { env: { PORT: '3003' } } } | |
events.js:141 | |
throw er; // Unhandled 'error' event | |
^ | |
Error: spawn node ENOENT | |
at exports._errnoException (util.js:837:11) | |
at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32) | |
at onErrorNT (internal/child_process.js:344:16) | |
at doNTCallback2 (node.js:429:9) | |
at process._tickCallback (node.js:343:17) | |
at Function.Module.runMain (module.js:477:11) | |
at startup (node.js:117:18) | |
at node.js:951:3 | |
27 Oct 12:50:44 - [nodemon] app crashed - waiting for file changes before starting... | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var app = require('../app'); | |
var debug = require('debug')('file-upload:server'); | |
var http = require('http'); | |
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* Module dependencies. | |
*/ | |
var process = require('process'); | |
var child_process = require('child_process'); | |
var ports = ['3000', '3001', '3002', '3003']; | |
ports.forEach(function(port) { | |
var oldSpawn = child_process.spawn; | |
function newSpawn() { | |
console.log('my spawn'); | |
console.log(arguments); | |
return oldSpawn.apply(this, arguments); | |
}; | |
child_process.spawn = newSpawn; | |
var child = newSpawn('node', ['./bin/server'], { env: { PORT: port}}); | |
child.stdout.on('data', function() { | |
console.log('data', arguments); | |
}); | |
child.on('exit', function() { | |
console.log('child exit with code', arguments); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment