Reputation: 3012
I am pretty new to this. Cannot figure out what this is. It says Port 3000 already in use but when I ping the port, it says unknown host. The static page contained nothing at all and it was working fine before I tried to do something with mongoose. Even after I removed the code, the error still persists. The only code I added to app.js was:
http://code.runnable.com/U0MpcpIzdaRkX2VH/querying-mongodb-from-express-using-mongoose-for-node-js
var express = require('express');
// Mongoose import
var mongoose = require('mongoose');
// Mongoose connection to MongoDB (ted/ted is readonly)
mongoose.connect('mongodb://ted:[email protected]:61797/theenlighteneddeveloper', function (error) {
if (error) {
console.log(error);
}
});
// Mongoose Schema definition
var Schema = mongoose.Schema;
var UserSchema = new Schema({
first_name: String,
last_name: String,
email: String
});
// Mongoose Model definition
var User = mongoose.model('users', UserSchema);
// Bootstrap express
var app = express();
// URLS management
app.get('/', function (req, res) {
res.send("<a href='/users'>Show Users</a>");
});
app.get('/users', function (req, res) {
User.find({}, function (err, docs) {
res.json(docs);
});
});
app.get('/users/:email', function (req, res) {
if (req.params.email) {
User.find({ email: req.params.email }, function (err, docs) {
res.json(docs);
});
}
});
// Start the server
app.listen(80);
Here's the screenshot of the terminal when I do an npm start.
This is the actual code in the app.js now. 80 is not listening now, 3000 is - which must be the default for express.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();
// 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(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
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;
Upvotes: 0
Views: 4596
Reputation: 588
Might be internally someother process is using the port 3000. Try with someothe port.
eg:
app.listen(3002);
Upvotes: 1
Reputation: 74899
I'm not sure why your app is complaining about port 3000
when you have configured it to use 80
, but something is already running on that port when you start up the Express app, most likely another instance of an express application.
To find if there are any other processes, run sudo lsof -Pni | grep 3000
e.g.
○ → sudo lsof -Pni | grep 3000
ruby 21742 user 10u IPv4 0x43010e29a7f402e5 0t0 TCP *:3000 (LISTEN)
On my system there is a ruby
process with pid 21742
owned by user
listening on TCP port 3000
If you don't find a running process then it might be code related.
Upvotes: 1