I am wondering if someone could help me point out how to cache a resource in express/node on startup of the web server. The myCol.find is very expensive, so I would like to just run it once on startup, and cache the result for all subsequent requests. Is there a startup step I can tie into? Can this be done synchronously before the server starts accepting requests?
I have the code below, but would like to reference a cached variable instead of the mongo db
var express = require('express');
var app = express();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
var myCol = require('./customModule');
var port = process.env.PORT || 8080;
var router = express.Router();
router.get('/test/:testId', function(req, res) {
myCol.find(function(err, allResults) {
res.json(allResults);
});
});
app.use('/api', router);
app.listen(port);
Yep, you just need to make sure your server starts listening after you've fetched your results through myCol.find():
var express = require('express');
var app = express();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
var myCol = require('./customModule');
var port = process.env.PORT || 8080;
var router = express.Router();
var myResultsCache;
router.get('/test/:testId', function(req, res) {
res.send(myResultsCache);
});
app.use('/api', router);
myCol.find(function(err, allResults) {
//you should add some error handling here
myResultsCache = allResults;
app.listen(port);
});
You could swap the order of myCol.find and the route handler, so that the relevant code would be:
myCol.find(function(error, results) {
if (error) throw error;
router.get('/test/:testId', function (request, response) {
response.json(results);
});
});
This is still asynchronous, but the server would only be able to respond after the find operation has been done.
Related
I am using node + redis and I facing strange issue when ever I run my app , connect event of redis client is called multiple times automatically as written in redis.js file.
Below is my code Server.js:
var express=require('express');
var app=express();
var port=8000;
var path = require('path');
var logger=require('morgan');
var bodyParser = require('body-parser');
var router = express.Router();
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/',function(req,res){
res.send({message:"Welcome to nodejs APIS"});
});
var redisObj=require('./redis.js');
app.use('/redischeck',redisObj);
app.listen(port,function(err,res){ if(err){ console.log("Server error");}else{console.log("Server running on port 8000");}});
redis.js
var express = require('express');
var router = express.Router();
var redis = require("redis");
var client = redis.createClient();
client.on('connect', function() {
console.log('connected'); // Prints multiple time in console
});
router.get('/', function(req, res) {
client.on("error", function (err) { console.log("Error " + err);});
client.set("foo", "bar", function (err, reply) {
client.quit();
res.json({status:'Success'});
});
});
module.exports=router;
I also cross checked this issue using 'netstat -na | grep 6379'.My observation were many connection were created and then went in to TIME_WAIT state which was strange because I just ran my app on my localhost without anyone connecting it from some other end.
Am I doing something wrong in code.
It was a mistake from my side I had changed timeout values in redis.conf file located at /etc/redis/redis.conf so that redis connection does not go in TIME_WAIT state for 60 sec.
I'm trying to use simplewebrtc in my app, I already have a simple nodejs server with express web framework. But to use simpleWebrtc we have to install signal master. I'm looking at the source code for the server.js file in the signal master package but I can't figure out how to combine this server.js with my already existing app.js file. This is basically my app.js
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongoose = require('mongoose');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
console.log("connected to index");
});
and this is server.js of signalMaster
/*global console*/
var yetify = require('yetify'),
config = require('getconfig'),
uuid = require('node-uuid'),
crypto = require('crypto'),
fs = require('fs'),
port = parseInt(process.env.PORT || config.server.port, 10),
server_handler = function (req, res) {
res.writeHead(404);
res.end();
},
server = null;
// Create an http(s) server instance to that socket.io can listen to
if (config.server.secure) {
server = require('https').Server({
key: fs.readFileSync(config.server.key),
cert: fs.readFileSync(config.server.cert),
passphrase: config.server.password
}, server_handler);
} else {
server = require('http').Server(server_handler);
}
server.listen(port);
var io = require('socket.io').listen(server);
if (config.logLevel) {
// https://github.com/Automattic/socket.io/wiki/Configuring-Socket.IO
io.set('log level', config.logLevel);
}
etc, etc you can look at the rest by downloading the zip. I thought it would be just replacing server with http, but the server=null doesn't really make sense. All the dependencies are in the directory of the signalMaster unzipped file. I was reading about signalMaster here.
You will need something like this
var os = require('os');
var static = require('node-static');
var http = require('http');
var socketIO = require('socket.io');
var fileServer = new(static.Server)();
var app = http.createServer(function (req, res) {
fileServer.serve(req, res);
}).listen(2013);
var io = socketIO.listen(app);
io.sockets.on('connection', function (socket){
...
socket.on('join', function (message) {
...
}
...
}
i hope this help u
I've created a node application with express. I try to separate the following layers which will give me the ability to test the application with unit testing...
The problem is that I don't know how to call to the router.js file which will stops in the post/get/delete application.
The server.js file looks as follows
http = require('http'),
app = require('./app')(),
http.createServer(app).listen(app.get('port'), function (err) {
console.log('Express server listening on port ' + app.get('port'));
});
This is the app.js file
var express = require('express'),
logger = require('morgan'),
bodyParser = require('body-parser'),
routesApp = require('./ro/route');
module.exports = function () {
var app = express();
app.set('port', process.env.PORT || 3005);
app.use(logger('dev'));
app.use(function (req, res, next) {
res.set('APP', 'User app');
next();
});
app.use(bodyParser.json());
app.use(routesApp);
return app;
};
This is the router.js, which will route the call to other module according to the http type like post/delete/get etc...
var handleGet = require('../controller/handleGet');
var handlePost = require('../controller/handlePost');
var express = require('express');
module.exports = function (app) {
var appRoute = express.Router();
app.use(appRoute);
appRoute.route('*')
.post(function (req, res) {
handlePost(req, res);
})
.get(function (req, res) {
handleGet(req, res)
})
Currently I've two questions:
How to make it work since when in debug It dump in
app.use(appRoute); on the router.js file?
The error is TypeError: undefined is not a function
Is it good way to structure the node app like in my post? I want to seperate all this layers like SOC, I'm fairly new to node and express and I try to build it to be modular and testable...
How to make it work since when in debug It dump in app.use(appRoute); on the router.js file? The error is TypeError: undefined is not a function
This fails because you don't pass app into the module when you require it in app.js, you would need to do something like
app.use(routesApp(app)); // <- this hurts my eyes :(
Is it good way to structure the node app like in my post?I want to sperate all this leyrs like SOC,I fairly new to node and express and I try to build it to be modular and testable...
Your definitely on the right track, keeping things separated is generally always a good idea. Testing is definitely one of the big pluses but it also helps with other things like maintainability & debugging.
Personally, I would make use of the bin directory for any start up script configuration
bin/www
var app = require('./app');
app.set('port', process.env.PORT || 3005);
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
This will help decouple your express app from all the environment setup. This should keep your app.js clean and only contain app-related config
app.js
var express = require('express')
, app = express()
, logger = require('morgan')
, bodyParser = require('body-parser')
, routes = require('./routes.js');
app.use(logger('dev'));
app.use(function (req, res, next) {
res.set('APP', 'User app');
next();
});
app.use(bodyParser.json());
app.use('/', routes);
...
module.exports = app;
Then finally, your routes.js should do nothing but handle your URLs
routes.js
var express = require('express')
, router = express.Router()
, handleGet = require('../controller/handleGet')
, handlePost = require('../controller/handlePost');
router.get('/', handleGet);
router.post('/', handlePost);
...
module.exports = router;
I'm currently working with Node.js, Express.js and Jade. My database is MySQL. I'm new to node.js, so I thought I try something very easy: Displaying some data from the database in a table in the browser.
Unfortunately it still doesn't work. I can display data on an free port but not where I need it - on port 3000. And I also can't work with the response itself. This is one of the "solutions" or ideas I had. Maybe there is a problem with the asynchronous call? I simply have no idea.
Here is my code:
routes.js
var express = require('express');
var controller = express.Router();
var dataModel2 = require('../models/rooms');
controller.get('/rooms', function(req, res, next) {
var rooms = dataModel2();
res.render('rooms', {
items: rooms
});
});
module.exports = controller;
models/rooms.js
var rooms;
var connection = require('./databaseConnection');
var http = require('http');
rooms = function() {
http.createServer(function (request, response)
{
console.log('Creating the http server');
connection.query('SELECT * FROM rooms', function(err, rows, fields)
{
response.writeHead(200, { 'Content-Type': 'application/json'});
var room = response.end(JSON.stringify(rows));
return room;
});
});
module.exports = rooms();
models/databaseConnection.js
var mysql = require('mysql');
module.exports = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'raspi_key_royal'
});
rooms.jade
extends layout
block content
div(id="bodyRoyal")
table(border='1')
thead
tr
th ID
th Name
tbody
each item in items
tr
td=item.rid
td=item.name
I splitted the functions a bit because there are some other sections like "persons" etc. I tried to insert console.logs in the rooms.js but that doesn't seem to work.
I also thought I could save the response into a variable so that I can work with it somewhere else.
Thank you for every help and hints!
Steffi
Something like this should do it:
var express = require('express'),
app = express(),
connection = require('./databaseConnection');
app.get('/rooms', function (req, res) {
connection.query('SELECT * FROM rooms', function(err, rows, fields)
{
res.render('rooms', {
items: rows
});
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
This is from the express site...http://expressjs.com/starter/hello-world.html
app.listen(3000, ...
is the how to configure a it to a specific port (in this case 3000).
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
I'm trying to modularize my application files and I'm having problems with Socket.io. I would like to use the io inside my routes.js. Something like this:
var router = require('express').Router();
var io = require('./sockets/my-io');
router.get('/', function(req, res) {
io.emit('request-detected');
});
module.exports = router;
But I can't do, because the socket.io needs the app server, and when I'm inside the routes.js file, the app server is not listening or being exported yet.
Can you give me a solution, or any other approach to this problem?
Here's what I have, and if it's possible, I would like to keep the file structure:
app.js
var app = require('express')();
var routes = require('./routes');
/* ... */
app.use('/contacts', routes);
module.exports = app;
bin/www
#!/usr/bin/env node
var app = require('../wallet');
var server = app.listen(port, function() {
debug('Express is listening o port ' + port);
});
routes.js
var router = require('express').Router();
router.get('/', function(req, res) {
console.log('hey');
});
module.exports = router;
You can do it by passing the io variable to your routes module.
bin/www
#!/usr/bin/env node
var app = require('./app');
var server = app.listen(3000, function() {
console.log('Express is listening on port 3000');
}); // start the server
var socket = require('./socket')(server); // require socket.io code
var routes = require('./routes')(socket); // require routes
app.use('/', routes);
app.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.set('views engine', 'ejs');
app.set('views', __dirname + '/');
module.exports = app;
socket.js
var socketio = require('socket.io');
function init(server) {
var io = socketio(server);
io.on('connection', function (socket) {
console.log("socket connected");
socket.on('newEvent', function (data) {
console.log(data);
});
});
return io;
}
module.exports = init;
routes.js
var express = require('express');
var route = express.Router();
function init(io) {
route.get('/', function (req, res) {
res.render('index.ejs', {});
setTimeout(function() {io.emit('newEvent', {message: "Hi from the server"})}, 2000);
});
return route;
}
module.exports = init;
The code above worked for me. However, I'm not sure why you want to do that.
Inside the router, you still have full control of what you want to send to the user via html, so you can just add the data to the html directly.
The idea of socket.io is that you can send data between the client and back once he has loaded the html and established a connection to your server with socket.io.
As you can see in the routes.js, I had to add a timeout to the emit. This is because the socket event will be emit before the browser has reloaded the page. In my case the browser logged the event and then immediately refreshed, losing the data you just sent.
Another problem is that you don't know anything about the socket of the client that is requesting the page because he hasn't connected yet. This means that calling io.emit() will send the event to all connected sockets.
As I said, this really depends on what exactly you want to do.
EDIT:
Instead of updating your contacts using ajax, you can do that with socket.io.
socket.js
var socketio = require('socket.io');
function init(server) {
var io = socketio(server);
io.on('connection', function (socket) {
console.log("socket connected");
socket.on('newContact', function (data, callback) {
// add data.contactName to db
// after adding something, you use the callback to
// send the added data back to the client
// callback(newContact);
});
});
return io;
}
module.exports = init;
index.html
<script type="text/javascript" >
var socket = io();
// call this emit when the user wants to add a contact
socket.emit('newContact', {contactName: name}, function(newContact) {
// here you will get the result from the server and you can
// update the html with jquery for example
});
</script>
If i understand your question correctly ,maybe you can try this way.
in your routes.js file
var app = require('./app');
var server = require('http').createServer(app);
var io = require('./sockets/my-io')(server);
var route = app.Router();
in your app.js file
var port = process.env.PORT || 3000;
app.listen(port,function(){
console.log('server on port ' + port)
})