I am on windows. The local database is running fine, I can write to and find collections with the mongo command line tool. The local nodejs is running fine as well. When I remove my mongoose code, I can reach my endpoints.
When I start the node server with the mongoose code, I get this message:
23:05:57 web.1 | started with pid 20860
23:05:58 web.1 | { [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
23:05:58 web.1 | js-bson: Failed to load c++ bson extension, using pure JS version
23:05:58 web.1 | Node app is running at localhost:5000
The server starts, although the error is worrisome (have tried to fix it for a good while, no luck yet), but it doesn't seem like a fatal error.
When I try to access one of my endpoints, either through Postman API, the browser or my application, it tries to connect forever. I can also see in mongo that a connection to the db never is made.
This is the code I have right now:
var express = require('express');
var app = express();
var mongoose = require('mongoose');
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
mongoose.connection.on("open", function(ref) {
console.log("Connected to mongo server.");
return start_up();
});
mongoose.connection.on("error", function(err) {
console.log("Could not connect to mongo server!");
return console.log(err);
});
mongoose.connect("mongodb://localhost/test");
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'));
});
app.get('/', function(request, response) {
response.send("Hello StackOverflow");
});
I've been sitting quite a few hours over the last three days trying to make it work, feels like I've been through every remotely relevant google search at this point. Hope you can help me out. :)
EDIT: If I go with mongoose version 3.8.3. The MODULE_NOT_FOUND error disappears. But it doesn't fix the never ending load.
Related
I am a beginner in Node js and Express and I was trying to create my server following this tutorial : https://codeforgeek.com/express-nodejs-tutorial/
This is my server.js code:
var express = require("express");
var app = express();
var server = app.listen(3000, function() {
console.log("We have started our server on port 3000");
});
error on cmd
It seems like it's the use of the ejs package without following the next steps of the tutorial, I get the same error at Step 3:
We have started our server on port 3000
/..stackhelp/59715144/node_modules/express/lib/application.js:119
this._router.handle(req, res, function(err) {
^
TypeError: Cannot read property 'handle' of undefined
at Function.app.handle (/Users/tamebadger/Projects/stackhelp/59715144/node_modules/express/lib/application.js:119:15)
at Server.app (/Users/tamebadger/Projects/stackhelp/59715144/node_modules/express/lib/express.js:28:9)
at emitTwo (events.js:125:13)
at Server.emit (events.js:213:7)
at parserOnIncoming (_http_server.js:602:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:116:23)
Removing the ejs package would solve your problem if it was an option (you need it later in the tutorial)
Our of interest sake, see if you still get the issue if you add the next part of the tutorial:
app.get('/',function(req,res){
res.send('Hello world');
});
I have a node application that is not working though did work about 2 hours ago (no code changes). Also no changes to the server, I only ran another node application on the same port but that process has been killed.
Command to start node node server
server.js
var app = require('./server/index');
app.set('port', process.env.PORT || 3000);
var server = app.listen(8080, function() {
console.log('Express server listening on port ' + server.address().port);
});
app.js (stripped down)
const express = require("express"),
mongoose = require("mongoose"),
app = express();
mongoose.connect(config.db, {autoReconnect: true}, (err) => {
if (!err) console.log('MongoDB has connected successfully.');
});
mongoose.connection.on('error', function() {
console.error('MongoDB Connection Error. Make sure MongoDB is running.');
});
var authRoutes = require('./routes/auth.js');
authRoutes(app, passport);
module.exports = app;
Output when starting node process
(node:4341) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:4341) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
MongoDB has connected successfully.
Solved it, seems that using node server was going directly into my server directory and running app.js file note the server.js file I wanted it to. Instead I had to run node server.js
I've got an AWS EC2 MEAN instance up and running (partially). The app is a RESTful JSON service and as far as I can tell is up and running as expected:
var app = require('./app');
var port = process.env.PORT || 3000;
var server = app.listen(port, function() {
console.log('Express server listening on port ' + port);
});
console output:
node server.js
Express server listening on port 3000
Db.prototype.authenticate method will no longer be available in the
next major release 3.x as MongoDB 3.6 will only allow auth against
users in the admin db and will no longer allow multiple credentials on
a socket. Please authenticate using MongoClient.connect with auth
credentials.
I've also added the Inbound Security Group for port 3000
testing the API out in the browser is where I run into problems... If I attempt to GET a list of objects using http://ec2-XX-XX-XX-XX.com:3000/belts the call eventually times out. However when I try a GET for a single object using http://ec2-XX-XX-XX-XX.com:3000/belts/some_id_here I get a valid 200 response with the expected object.
Of course everything works as expected locally. What am I missing?
Thanks in advance
//edit with requested code formatted :)
//app.js
var express = require('express');
var app = express();
var BeltController = require('./controller/BeltController');
app.use('/belts', BeltController);
//Belt Controller
router.get('/', function (req, res) {
Belt.find({}, function (err, belts) {
if (err) {
return res.status(500).send("There was a problem finding the Belt. " + err);
}
res.status(200).send(belts);
});
});
I want to prevent my NodeJS app from crashing and read that using domain is the way to do it. I'm still a little confused though but followed what I saw to get it set up. Is this set up correctly? Do I need to change anything? Thanks.
var express = require('express');
var http = require('http');
var path = require('path');
var d = require('domain').create();
var app = express();
app.configure(function() {
//set up express
app.set('port', process.env.PORT || 3000);
app.use(app.router);
app.use(function(req,res){
res.redirect('/error');
});
});
//launch
d.on('error', function(er) {
console.log('Error!', er.message);
});
d.run(function() {
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
});
When I create in error in one of my route files it seems to work correctly, but the error isn't logging. Should I have it log to a file or something so I can check for errors later on?
Well, what you're doing is a bad practice according to the NodeJS documentation, since you can start a memory leak every time the error occurs. There's an example on the domain api docs page of the right way to do it. I'm not sure why console.log isn't working, however -- a log file is probably a good idea. Also be aware that the api for domain is still unstable and may change.
In short you should handle your errors gracefully and start a new worker process when an unexpected an error occurs (using the cluster module to start and stop workers that run into problems).
From the docs, this is what you're not supposed to do, which looks awfully close to what you're doing:
// XXX WARNING! BAD IDEA!
var d = require('domain').create();
d.on('error', function(er) {
// The error won't crash the process, but what it does is worse!
// Though we've prevented abrupt process restarting, we are leaking
// resources like crazy if this ever happens.
// This is no better than process.on('uncaughtException')!
console.log('error, but oh well', er.message);
});
d.run(function() {
require('http').createServer(function(req, res) {
handleRequest(req, res);
}).listen(PORT);
});
I'm totally new with all the technologies I'm trying to do this with, but I have what seems like some simple code (gleaned from a tutorial) that I just can't get to work. I'm using Node, Express and Mongoskin/MongoDB. Whenever I try any operation against the db, I get a very generic "connection closed" error. I've got MongoDB 2.4.6, Mongoskin 0.6.0 and Mongo Native 1.3.19. MongoDB is running and I can connect from the terminal and work with my db. I see in the Mongo logging that my code never even establishes a connection. I thought maybe I need to call open explicitly, but even that returns the same error.
I'm sure I'm doing something dumb, but I'm stumped and help would be appreciated. Here's the code:
var express = require("express");
var mongoskin = require("mongoskin");
var db = mongoskin.db("localhost:28017/test", { safe: true, auto_reconnect: true });
var app = express();
app.get("/", function(request, response){
db.collection('testResult').find(function(error, result){
if (error) {
response.send("Find failed: " + error);
}
else {
response.send("got it ");
}
});
});
app.listen(8888);
Yep. I was doing something dumb. Just in case this is helpful for any other noob... The http client runs on port 28017 but MongoDB itself is actually listening on port 27017. Note the "7" in the second position. Duh. The right connection parameter (in my case), then, would be "localhost:27017/test".