NodeJS Require, where do I use this? - node.js

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?

Related

Multiple Routes in node express backend

I am new in node js and want to build an application in which they are multiple APIs and all are settled in routes folder but here is the problem when I tried to call the API in index.js(main file) its only take one route either userAPI or cleanerAPI.
As both APIs have different URL.
var mongoose = require ('mongoose');
var express = require('express');
const bodyParser = require('body-parser');
var app = express();
const routerUser= require('./routes/userAPI')
mongoose.connect('mongodb://localhost:27017/Covid');
app.use(bodyParser.json());
app.use('/',router);
app.listen(4000,function(){
console.log("Server Running on 4000")
});
you can try this one also.
Main file(index.js or server.js)
const appRoutes = require('./Routes')
appRoutes(app)
Routes(index.js)
module.exports = app => {
app.use("/user", require('./users'));
app.use("/cleaner", require('./cleaner')); };
Routes(users.js)
const express = require('express');
const router = express.Router();
router.route('/list')
.post(function(req,res){......});
module.exports = router;

NodeJS - Socket.io Connection to Routes

I am trying to connect socket.io to my routing system, but despite the multiple methods I have read about to achieve this, I'm not sure the best approach with my setup as I have three degrees of separation from my server file and the route file that requires the socket.io configuration. Should I pass it as a constructor argument?
I tried passing the argument //Routing app.use(controllers)(io);, but ran into an error with the router module, so I don't believe I'm setting it correctly.
Error: var search = 1 + req.url.indexOf('?'); TypeError: Cannot read property 'indexOf' of undefined
Here is my setup.
app.js:
var express = require('express');
var app = express();
//Port Setting
var port = process.env.PORT || 3000;
//Server Port
var server = app.listen(port);
//External Packages
var io = require('socket.io')(server);
var redisAdapter = require('socket.io-redis');
var controllers = require('./app/controllers/routes');
//Routing
app.use(controllers);
routes.js:
var express = require('express');
var router = express.Router();
router.use('/app', require('./app-routes'));
router.use('/api', require('./api-routes'));
module.exports = router;
app-routes.js (where I would like to configure socket.io):
var express = require('express');
var app = express();
var appRoutes = express.Router();
/*==== / ====*/
appRoutes.route('/')
.get(activityFeed.load)
.post(activityFeed.filter);

node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))

I am trying to create nodejs express application, I am facing weird issue while using routes
This is how my server.js looks
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const api = require('./server/routes/api');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use('/api',api);
app.use(express.static(path.join(__dirname,'dist')));
app.get('/',function(req,res){
res.sendFile(path.join(__dirname,'dist/index.html'));
});
const port = process.env.port || 3000;
app.set('port',port);
const server = http.createServer(app);
server.listen(port,function(){
console.log('server running at port '+port);
});
I am getting following error "node_modules/express/lib/router/index.js:458
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
"
Please let me know where I am going wrong
I just forgot to add module.exports = router in my api.js adding this solved my problem

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'
}

Resources