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;