currently developing a distributed system and crashed my code last night. My server-application has multiple modules which each established a connection to my redis db, however it would be better if there is a single connection via the main module. This, I tried to implement last night, But I can't quite figure out what exactly is the problem. I'm still quite new to this field, and already searched through pages like SitePoint, Medium, and other blogs, and also checked for my problem or derived versions of it here, but found nothing what could help me enough to settle this.
UPDATE 2016/6/24 13:18
I removed the app.use()-Statement from my code and now the server is firing up. Progress :)
However, If I try to test a route , the only reply I get is Cannot <METHOD> <ROUTE>.
Looking at this question here provides me with the answer to utilize the very app.use() I just removed from the code. Is there something I'm missing?
UPDATE 2016/6/24 14:05
I implemented the statement app.use('/', account_routing.routes);, doesn't work but seems the right way to go, doesn't it? I can send requests, but there is no answer whatsoever right now.
I'm providing the code for my server application:
main.js
var
express = require('express'),
redis = require('redis'),
app = express(),
port = process.env.port || 3000,
rclient = redis.createClient(),
account_routing = require('./modules/account_routing');
// ### CONFIGURATION ################################################
rclient.on('connect', function (err, res) {
console.log(err || "Connected to database!");
});
account_routing.setClient(rclient);
app.use('/', account_routing.routes);
// ### FIREUP #######################################################
app.listen(port, function () {
console.log('Server available at port ' + port);
});
account_routing.js
var rclient, express;
express = require('express');
module.exports = {
setClient : function (inClient) { 'use strict'; rclient = inClient; },
routes : function(app) {
// ### VARIABLES ####################################################
var bodyParser, http, morgan, redis, rclient, app;
// ### DEPENDENCIES #################################################
bodyParser = require('body-parser');
http = require('http');
morgan = require('morgan');
redis = require('redis');
app = express();
// ### CONFIGURATION ################################################
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// ### ROUTES #######################################################
app.get('/users/:username', function (req, res) {
/* Access database via connection in main module
to retrieve user data */
});
}
};
Thank you all very much in advance for help.
So basically after an awful lot of lost nerves, I stumbled upon this nice gem here.
I implemented it this very way, so now I have:
main.js
"use strict";
var
express = require('express'),
bodyParser = require('body-parser'),
app = express(),
port = process.env.port || 3000,
account_routing = require('./modules/account_routing')(app);
app.listen(port, function () {
console.log('Server available at port ' + port);
});
redis_db.js
var
redis = require('redis'),
rclient = redis.createClient();
rclient.on('connect', function (err, response) {
'use strict';
console.log("Connected to database");
});
module.exports = rclient;
and account_routing.js
'use strict';
module.exports = function (app) {
var
bodyParser = require('body-parser'),
http = require('http'),
morgan = require('morgan'),
rclient = require('./redis_db');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(morgan('dev'));
/* Routes */
This way, there is a single connection established to the redis database, and also the routes are working properly now.
I hope this information will help somebody.
Related
I know this is a simple problem but I have been looking for a solution for the last 2 days and can't seem to find it. I am following a tutorial where we've set up an express server. I think the two relevant files are app.js and auth.js.
APP.js
//PACKAGES
//"mongodb://0.0.0.0:27017/test"
require('dotenv').config()
const mongoose = require('mongoose')
const express = require("express")
const app = express()
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const cors = require('cors')
//LOCAL PACKAGES
const authRoutes = require("./routes/auth")
//CONNECTING TO MONGODB
mongoose.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex:true
}).then(() => {
console.log(`DB CONNECTED!!!!!`)
})
//MIDDLEWARE
app.use(bodyParser.json)
app.use(cookieParser())
app.use(cors())
//ROUTES
app.use("/api", authRoutes)
//PORT
const port = process.env.PORT || 8000;
//STARTING A SERVER
app.listen(port, ()=> {
console.log(`app is running at ${port}`)
})
AUTH.js
const express = require('express')
const router = express.Router()
router.get("/signout", (req,res)=> {
res.send("user signout")
})
module.exports = router;
In the console, I see that my server is connected to DB and running on port no 8000
However, when I go to the browser and write http://localhost:8000/api/signout, a spinning wheel that never stops and it does not return my request. I have tried to match the author's code, turn off the firewall, and changed the port number but nothing works. I would be grateful if someone helps as I am stuck on this problem and I want to progress. Thank you!
P.S: my github repo: https://github.com/timothyroybd/ecommerce-website
There is a problem with usage of body-parser.
You have app.use(bodyParser.json)
and should be app.use(bodyParser.json())
I've got a simple Express server that uses the body-parser module to access POST-parameters. The app looks like this:
/index.js:
'use strict';
const express = require('express');
const app = express();
const apiRouter = require('./api/routes.js');
// Set our port for the server application
const port = process.env.PORT || 8080;
// Register the routes for the /api prefix
app.use('/api', apiRouter);
// Start server
app.listen(port);
console.log('The server is running on port ' + port);
/api/routes.js:
'use strict';
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
// Configure app to use bodyParser(). This will let us get the data from a POST
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());
// START ROUTES
router.post('/devices', (req, res) => {
console.log(req.body); // Returns {}
res.json(req.body);
});
module.exports = router;
The problem is that the req.body object is empty (Always returns an empty object {}). Since I already loaded the body-parser middleware I have no idea what else I can try. I hope you have any suggestions.
I used the app Postman for testing. It appeared that it sent the POST data as form-data instead of x-www-form-urlencoded. After changing this setting the data showed up.
I've been trying to understand how to set up Stripe for my app but am having problems with the implementation of the module. Normally when using a module i would require it in the top of the file to be able to use it but I'm not sure how to do this here in the paymentController file or if i even need to. I imported the Stripe npm, so does that i mean that i can access it globally? Well as you see i'm quite new to this and would like to understand how to structure this so that the payments work.
app.js file:
angular.module('userApp', ['appRoutes', 'userControllers', 'userServices', 'ngAnimate', 'mainController', 'authServices', 'managementController', 'paymentController'])
.config(function($httpProvider) {
$httpProvider.interceptors.push('AuthInterceptors');
});
paymentController file:
angular.module('paymentController', [])
.controller('paymentCtrl', function($scope) {
var app = this;
});
Server.js file:
var express = require('express'); // ExperssJS Framework
var app = express(); // Invoke express to variable for use in application
var port = process.env.PORT || 8080; // Set default port or assign a port in enviornment
var morgan = require('morgan'); // Import Morgan Package
var mongoose = require('mongoose'); // HTTP request logger middleware for Node.js
var bodyParser = require('body-parser'); // Node.js body parsing middleware. Parses incoming request bodies in a middleware before your handlers, available under req.body.
var router = express.Router(); // Invoke the Express Router
var appRoutes = require('./app/routes/api')(router); // Import the application end points/API
var path = require('path'); // Import path module
var passport = require('passport'); // Express-compatible authentication middleware for Node.js.
var social = require('./app/passport/passport')(app, passport); // Import passport.js End Points/API
app.use(morgan('dev')); // Morgan Middleware
app.use(bodyParser.json()); // Body-parser middleware
app.use(bodyParser.urlencoded({ extended: true })); // For parsing application/x-www-form-urlencoded
app.use(express.static(__dirname + '/public')); // Allow front end to access public folder
app.use('/api', appRoutes); // Assign name to end points (e.g., '/api/management/', '/api/users' ,etc. )
mongoose.connect('mongodb://localhost:27017/tutorial', function(err) {
if (err) {
console.log('Not connected to the database: ' + err);
} else {
console.log('Successfully connected to MongoDB');
}
});
// Set Application Static Layout
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + '/public/app/views/index.html')); // Set index.html as layout
});
// Start Server
app.listen(port, function() {
console.log('Running the server on port ' + port); // Listen on configured port
});
I'd recommend following what's shown for Node in this Stripe tutorial:
https://stripe.com/docs/charges
Just like your other includes, you want something like this at the top of any JS file that will use the Stripe library:
var stripe = require('stripe')('sk_my_secret_key')
Then, elsewhere in the same file, you can call any Stripe library methods you need:
stripe.charges.create(…)
Of course, in production you'll want to build a proper 12 Factor app[1] and put your secrets in environment variables or configuration files.
[1] https://12factor.net/config
I am building an api that will interface with the MongoDB database and have mounted it as a subapplication. I have defined a session variable in my server controller.
However, any time that the server files need to talk to the api files the session variables are never passed off.
Heres the app.js file
//app.js file
'use strict';
process.env.NODE_ENV = 'development';
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var flash = require('connect-flash');
var helmet = require('helmet');
var app = express();
app.use(helmet());
var port = process.env.PORT || 3000;
mongoose.connect("mongodb://localhost:27017/striv4");
var db = mongoose.connection;
// mongo error
db.on('error', console.error.bind(console, 'connection error:'));
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: db
})
}));
app.use(flash());
// make user ID available in templates
app.use(function (req, res, next) {
res.locals.currentUser = {
username:req.session.username,
id: req.session.userId
};
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser('secreter'));
app.use(logger('dev'));
var api = require('./app_api/routes/index');
var serverRoutes = require('./server/routes/index');
//static file middleware
app.use(express.static(__dirname + '/public'));
app.set('views',__dirname +'/server/views');
app.set('view engine','pug');
app.use('/',serverRoutes);
app.use('/api',api);
//custom error handler
app.use(function(error, req, res, next) {
res.status(error.status || 500);
res.send('Error: '+error.message);
});
app.listen(port);
console.log('Listening on port: '+port);
You've got the whole program listed so there is more than one way for this to have gone wrong. Here are my suggestions to fix this:
Check the version of express-session you've installed. (Just run npm ls in the terminal and in your root Node app folder where you're package.json file is). If it's equal to or greater than v1.5.0, you don't need the cookie-parser for sessions anymore. Comment out the app.use line for the cookie parser and see if this works.
If you still need cookie parser for some other reason, you should use the same secret for sessions and the cookie parser. In your code, you've set two different values for secret.
I've seen that the other big failure for sessions occurs if the session store is not correctly connected to your Node app. Cross-check that the database is available and working. In my experience, Express sessions will fail silently if it can't get to the DB.
Hope this helps.
This is a question other people have asked, but I can't manage to benefit from the answers they've been given, due to the different Express setup I have.
I've got socket.io implemented and working in a simple way on my server. This is how it works:
In bin/www:
#!/usr/bin/env node
var debug = require('debug')('gokibitz');
var app = require('../../server');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
var io = require('socket.io').listen(server);
io.on('connection', require('../routes/socket.js'));
All my routes and other Express setup is in ../../server.js.
var routes = require('./server/routes/index');
var user = require('./server/routes/user');
...
app.use('/', routes);
app.use('/api/user/', user);
...
app.use('*', routes);
Then in ../routes/socket.js, I've got this boilerplate:
module.exports = function (socket) {
socket.emit('send:news', { hello: 'world' });
setInterval(function () {
socket.emit('send:time', {
time: (new Date()).toString()
});
}, 1000);
return socket;
};
This is working beautifully, I might add. But now I want to be able to emit events from the various routes in my quite-ordinary Express app, and I can't for the life of me figure out the right way to get a reference to the socket object I need.
Example: when a user makes a comment, I'd like to emit an event to all connected users notifying them of the new comment. From my routes file (./server/routes/user.js), how can I get access to the object I need to emit events?
Here's a skeleton of the relevant bits from a route file:
var express = require('express');
var router = express.Router();
router.post('/', function (req, res) {
...
});
module.exports = router;
The only place I can access it is in the ../routes/socket.js file, which is useless.
All of the routes are set in a app.js before there's any io or socket object to pass in.
Should I be able to require('socket.io') and use it somehow to emit to all connected sockets?
Is there a sensible way to store the connected sockets on ../routes/socket.js so it can be required and emitted to from other routes?
Can anyone steer me in the right direction?
I was able to ultimately get things working using this example:
https://github.com/expressjs/generator/issues/45#issuecomment-53719435
I created a new file called server/io.js:
var io = require('socket.io')();
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
module.exports = io;
Then I updated server/bin/www to this:
#!/usr/bin/env node
var debug = require('debug')('gokibitz');
var app = require('../../server');
var io = require('../io');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
io.attach(server);
Then, in my route, I use this:
var io = require('../io');
...
io.emit(...);
The missing piece for me, at least, was the ability to create the io object separately, return the correct object, then use io.attach(server) inside bin/www to start it up at the right point.
Hopefully this will help someone else following in my footsteps.
I think you are confused with the concepts. The socket.io lib uses or emulate a websocket (bidirectional socket) with the client, and have not relation with routes.
You can send a notification to all sockets using the io object:
io.emit('message_to_all', true);
You have to an array on io.sockets, with all sockets.
You can uses namespaces or rooms to, I recomend you learn the documentation:
http://socket.io/docs/rooms-and-namespaces/#
Add something:
If you want to send a message to all people in the same route, you can join to a channel with the same name of the path.
For example, in the client:
var socket = io(window.location.href); // Or route..
And the server:
var nsp = io.of('/the/specific/route');
nsp.on('connection', function(socket){
// ...
});
nsp.emit('message_to_all_in_route', data);
About the last question editing:
You can send the io object in request or response object to routes, using the Express midleware API:
For example:
#!/usr/bin/env node
var debug = require('debug')('gokibitz');
var app = require('../../server');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
var io = require('socket.io').listen(server);
app.use(function (req, res, next) {
res.io = io;
next();
});
io.on('connection', require('../routes/socket.js'));
And in the route:
route.post('/post/:id/comments', function (req, res) {
// Do your logic
var postio = res.io.of('/post/' + req.params.id);
postio.emit('new_comment', commentData);
});
As I said in my comment, you can send to all connected clients with:
io.emit(msg, data);
This will require access to the io object that you created in your first module. The usual way to share that with your module with your routes modeule would be to export a method from the routes module that lets you pass it the io object and then after you've required in the routes module and created the io object, you can just call that method to pass the io object to the routes module and it can save it locally for future use.
io is just a shared object like the app object is. If you want a module to be able to use a shared object, the usual way is that you call some method in that module to share with it some objects that it can then use.
I have made a new file sockets.js in that file:
var socketio = require('socket.io');
var io;
module.exports = {
socketServer: function (app) {
io = socketio.listen(app);
io.on('connection', function (socket) {
console.log(Object.keys(users));
socket.on('message', function (data) {
io.emit('updateChat', 'Hello World!')
});
});
}
}
And in my app.js:
var express = require('express');
var app = express();
var http = require('http').createServer(app);
var port = process.env.PORT || 3000;
var io = require('./sockets').socketServer(http);
http.listen(port, function () {
console.log('SERVER RUNNING.. PORT: ' + port)
});
This is working for me. Good Luck!