Error connecting to MongoDB: connect ECONNREFUSED 127.0.0.1:27017 - node.js

I try to connect to MongoDB, but I get the error. Here is my code and the error below.
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var port = process.env.PORT || 8080;
var database = require('./config/database');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
mongoose.connect(database.localUrl);
app.use(express.static('./public'));
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({'extended': 'true'}));
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse
application/vnd.api+json as json
app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-
HTTP-Method-Override header in the request
require('./app/routes.js')(app);
app.listen(port);
console.log("App listening on port " + port);
Heres the error image

Related

Browser showing "Cannot GET /api/posts/ " instead of "hello" from res.send('hello')

My index.js file:
//Dependencies
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const posts = require('./routes/api/posts.js');
//Configuration
const port = process.env.PORT || 5000;
//App object
const app = express();
//Middleware
app.use(bodyParser.json());
app.use(cors());
//Main app
app.use('api/posts',posts);
//Starting server
app.listen(port,()=>{
console.log(`server running at ${port}`);
});
My Api file:
//Dependencies
const express = require('express');
const mongodb = require('mongodb');
//Mini app
const router = express.Router();
//Get post
router.get('/',(req,res)=>{
res.send('hello');
});
//Add post
//Delete post
module.exports = router;
I'm expecting to get "hello" in my browser but constantly getting "Cannot GET /api/posts/" in firefox and postman. What should I do now?
Correction :-
//Main app
app.use('/api/posts',posts);

NodeJs route not working

I am new to NodeJs. I am getting error with below code.
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
port = process.env.PORT || 9090,
mongoose = require('mongoose'),
Cheque = require('./models/cheque'),
router = express.Router();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
mongoose.connect('http://localhost:27017/utils/chequeman');
router.route('/cheques').post(function (req, res) {
console.log('u r in cheques.');
var cheque = new Cheque();
cheque.chequeReceiptDate = req.body.chequeReceiptDate;
cheque.save(function (err) {
if (err)
res.send(err)
res.json({ message: 'Cheque details added' });
});
});
app.use('/api', router);
app.listen(port);
console.log('Magin happens at' + port);
When I am trying POST request in postman I am getting response as "Cannot POST /api/cheques"
Please help.
Try to declare the variables that you are using.
let express = require('express');
let app = express();
let bodyParser = require('body-parser');
let port = process.env.PORT || 9090;
let mongoose = require('mongoose');
let Cheque = require('./models/cheque');
let router = express.Router();
Take your API static
app.use('/CHEQUEDIRECTORY', express.static(__dirname + '/CHEQUEDIRECTORY'))
Don't forget to export modules on you API.
Have you installed the packages correctly?

Node and Express Routing - 404 error

I'm getting back 404 error using nodemon and not quite sure where the problem is. Any tips / resources appreciated!
project root
$ curl http://127.0.0.1:3000/v1/protected
Cannot GET /v1/protected
nodemon: "GET /v1/protected/ HTTP/1.1" 404 26 "-" "curl/7.49.1"
index.js
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();
var router = require('./services/router');
mongoose.connect('mongodb://localhost:introToBackend/introToBackend');
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(express('/v1', router));
const PORT = process.env.PORT || 3000;
var HOST = process.env.HOST || '127.0.0.1';
console.log('Listening on', HOST, PORT);
app.listen(PORT, HOST);
services/router.js
var router = require('express').Router();
function protectedRoute(req, res, next) {
res.send("The secret!");
}
router.route('/protected')
.get(protectedRoute);
module.exports = router;
Try writing
express.use('/v1',yourRouter);
You do not need the express inside app.use

How to check mongo database is connected?

I have mongodb up and running for the application how can i make sure when application start it is connected with db ?
app.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var mongoose = require('mongoose');
console.log(mongoose.connection.readyState);
var db = require('./config/db');
var port = process.env.PORT || 8080;
mongoose.connect(db.url);
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app); // configure our routes
app.listen(port);
console.log('listening on port ' + port);
exports = module.exports = app;
config > db.js
module.exports = {
url : 'mongodb://localhost/test-dev'
}

NodeJS Require, where do I use this?

Node JS require for the bittrex api... exactly where do it.
npm everything is installed including require...
var bittrex = require('bittrex-api');
on any page gives require is not defined
1) I'm using ui-router, server.js is not needed... I added one anyway.
var express = require('express');
var path = require('path');
//var logger = require('morgan');
//var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
//var bcrypt = require('bcryptjs');
var app = express();
app.set('port', process.env.PORT || 8080);
//app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
//app.use(cookieParser());
app.use(express.static(path.join(__dirname, '/')));
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
now I want to add the bittrex ticker on the dashboard... so on the dashboard view I add
var market = 'BTC-LTC';
var bittrex = require('bittrex-api');
I turn on the server.js file and I get the require undefined message, I turn it off same message.
What am I doing wrong?

Resources