The thing I want to do is modify the response body.
For this I am using a middleware that gets called at every request.
To achieve it, I took a demo application from github https://github.com/ccoenraets/nodecellar . I added a middleware in the server.js similar to the example given on express logging response body.
Still I am unable to modify the response body, as res.send = function (string) does not get called.
Below mentioned is the code. Please let me know what wrong am I doing here.
var express = require('express'),
path = require('path'),
http = require('http'),
wine = require('./routes/wines');
var app = express();
app.configure(function () {
app.set('port', process.env.PORT || 4000);
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser()),
app.use(express.static(path.join(__dirname, 'public')));
app.use(logResponseBody);
});
app.get('/wines', wine.findAll);
app.get('/wines/:id', wine.findById);
app.post('/wines', wine.addWine);
app.put('/wines/:id', wine.updateWine);
app.delete('/wines/:id', wine.deleteWine);
http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
function logResponseBody(req,res,next){
var send = res.send;
console.log("send resp is: "+send);
res.send = function (string) {
var body = string instanceof Buffer ? string.toString() : string;
console.log("Body found is: "+body);
body = body.replace(/<\/head>/, function (w) {
return 'Modified head' + w;
});
send.call(this, body);
};
res.on('finish', function(){
console.log("Finished " + res.headersSent); // for example
console.log("Finished " + res.statusCode); // for example
})
next();
}
PS: I am starting a new thread for a similar question as I have less than 50 reputation.Therefore cant add comments there.
The best way to add middleware to an express app is to use the app.use method, so you can remove the whole http.createServer block and replace it for something like
app.use(function(req, res, next) {
//do everything you want to happen on every request
next();
});
app.listen(app.get('port'), function() {
console.log('listening on port ' + app.get('port'));
});
For more info on app.use, check the express app use documentation.
Related
I'm new to node and got stuck with handling multiple async tasks.
Except from node, I've got another server (S1) which doesn't return data immediately to requests, it can returns multiple types of data and also can send notifications without requesting them specifically, so node have to listen to data from it , parse it and act accordingly.
The connection to this server (S1) is done by using:
S1 = net.createConnection({'host':S1Host, 'port': S1Port});
And node listens to data with:
S1.on('data', function(data){
S1DataParse(data);
});
I have to route the correct data (after parsing it) to a specific POST request.
app.post('/GetFooFromS1', function(req, res){
// Send request to S1
S1.write({'type':'foo'});
// If got the correct data sometime in the future, send response to the browser
res.setHeader('Content-Type', 'application/json');
res.json({'status':'success', 'value':S1FooData});
});
I tried to use the async module for that, but with no success.
What I was trying to do:
var asyncTasks = [];
app.post('/GetFooFromS1', function(req, res){
asyncTasks.push(function(callback){
// Send request to S1
S1.write({'type':'foo'});
});
async.parallel(asyncTasks, function(response){
res.setHeader('Content-Type', 'application/json');
res.json({'status':'success', 'value':response});
});
});
and another task in S1DataParse:
function S1DataParse(){
if(data.type='foo'){
asyncTasks.push(function(callback){
callback(data);
});
}
}
But, of course, the second task never added to the asyncTasks array. I really got stuck with that.
Can you please help me with that?
Thanks
-=-=-=- Edit -=-=-=-
Eventually, I came accross with events and EventEmitter().
From the POST request I call the function that sends requests to the data server (DataServerClientGet).
In this function I register a listener which will get the future data.
eventEmitter.on('getData', returnDataServerData);
It all works great except for one thing. Whenever I refresh the page or add other POST requests, I get an error:
Error: Can't set headers after they are sent.
It would be great if I solve this problem. Help me, please.
Thanks ;)
The whole code looks like this:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var bodyParser = require('body-parser')
var net = require('net');
var events = require('events');
var dataServerHost = '127.0.0.1';
var dataServerPort = 12345;
var dataServerClient;
var logMsg;
var eventEmitter = new events.EventEmitter();
/*******************************************/
// Init
/*******************************************/
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + '/public'));
/*******************************************/
// Connect to the data server
/*******************************************/
DataServerConnect();
/*******************************************/
// Open listener on port 3000 (to browser)
/*******************************************/
http.listen(3000, function(){
logMsg = 'listening on *:3000';
console.log(logMsg);
});
/*******************************************/
// Routing
/*******************************************/
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.post('/GetDataFoo', function(req, res){
var msg;
var size;
msg ='\n{"Type":"Query", "SubType":"GetDataFoo","SearchFilter":""}';
size = msg.length;
logMsg = 'Client to DataServer: GetDataFoo';
console.log(logMsg);
DataServerClientGet('GetDataFoo', size, msg, res);
});
/*******************************************/
// Functions
/*******************************************/
function DataServerConnect(){
dataServerClient = net.createConnection({'host':dataServerHost, 'port': dataServerPort}, function(){
logMsg = 'Connected to DataServer ['+dataServerHost+':'+dataServerPort+']';
console.log(logMsg);
});
dataServerClient.on('data', function(data){
logMsg = 'DataServerData>>>\n'+data.toString()+'DataServerData<<<';
console.log(logMsg);
DataServerDataParse(data.toString());
});
dataServerClient.on('end', function(){
logMsg = 'Disconnected from DataServer';
console.log(logMsg);
});
}
function DataServerClientGet(type, size, msg, res){
dataServerClient.write('Type: Json\nSize: '+size+'\n\n'+msg, function(err){
var returnDataServerData = function returnDataServerData(results){
res.setHeader('Content-Type', 'application/json');
res.json({'status':'success', 'value':results});
}
eventEmitter.on('getData', returnDataServerData);
}
function DataServerDataParse(json){
if(json.Type=='GetDataFoo')
{
var MessageList = json.MessageList;
eventEmitter.emit('getData', MessageList);
}
}
-=-=-=- Edit -=-=-=-
The Error: Can't set headers after they are sent. caused by adding the same listener of the same type each time the DataServerClientGet was called and the res was sending multiple times.
I solved this one by adding: removeListener(event, listener)
right after the res, inside the function. Anyway, I think it's wrong and can cause problems if there will be multiple calling to DataServerClientGet with the same type etc.
There is a optional callback parameter that you can pass to write function(docs), something like :
S1.write({'type':'foo'},function(err){
if(err){
//Handle error
}else{
res.setHeader('Content-Type', 'application/json');
res.json({'status':'success', 'value':response});
}
})
This can work with post route , but in your 'data' listener ,you cant send data from server to client when there is not connection initialized by client (it is not bidireccional ) if you want bidireccional behavior you can check socket.io
I have this basic express app:
var express = require('express');
var app = express();
var PORT = 3000;
var through = require('through');
function write(buf) {
console.log('writing...');
this.queue('okkkk');
}
function end() {
this.queue(null);
}
var str = through(write, end);
/* routes */
app.get('/', function(req, res){
res.send("Hello!");
})
app.post('/stream', function(req, res){
var s = req.pipe(str).pipe(res);
s.on('finish', function() {
console.log('all writes are now complete.'); // printed the first time
});
});
/* listen */
app.listen(PORT, function () {
console.log('listening on port ' + PORT + '...');
});
When I post some data to /stream endpoint for the first time after starting the server I get okkk as the response which is what I expect. However, after that, any requests to /stream endpoint just timeout and not return any response.
Why is it so? What's exactly happening here?
I had this same problem and looks like res was not being finished properly. So I added a callback to my stream and ended que res myself. That fixed my problem:
stream.on('end', () => res.end());
stream.pipe(res);
It worked when I replaced req.pipe(str).pipe(res) with req.pipe(through(write, end)).pipe(res) which essentially makes sure that a new instance of through stream is created for every request.
i dont know why im getting this error:
this is my code
/*
Chat application for #node.js
express version.
*/
//Load modules.
var express = require('express'),
socket = require('socket.io'),
swig = require('swig'),
fs = require('fs');
//Load config.
console.log('Loading configuration.');
var config = fs.readFileSync('config.json');
var config = JSON.parse(config);
var port = config.port;
var views = config.views;
console.log('Configuration loaded.');
console.log(config);
//Initiate express module in app.
var app = express();
// app.get('/', function(request, response)
// {
// fs.readFile('./views/index.html', function(error, data)
// {
// if(error)
// {
// response.send('View cannot be rendered.');
// }
// response.type('html');
// response.send(data);
// });
// });
var test = "Hello";
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
swig.setDefaults(
{
cache: false
});
app.get('/', function(request, response)
{
response.render('index',
{
//Var to be named in the render : value;
'test': test,
'Title': 'Testing page',
});
});
//logger.
app.use(function(request, response, next)
{
console.log('%s %s', request.method, request.url);
next();
});
//Set directory for static files (css, js, img)
app.use(express.static(__dirname + '/public'));
//Run the app.
app.listen(port);
it might be some kind of error with the framework itself, but i dont get the clue why, i was using it last night with no error, now im getting this error this morning..
EADDRINUSE means another process is already listening on the same port. Probably this is an earlier run of your server that is still running/crashed and still using the port. Find that process, terminate it, and retry. This is not an error in your javascript code.
Also since you are binding port 80 and running windows, it could be that another web server such as IIS is running and using port 80.
The ports from 0 to 1024 are typically reserved for OS to use. Though its not a thumb rule not to use ports between these limits, its always safe to not use ports in this range to avoid accidental/occassional collision of using port#s.
I'm new to Socket.io and Express. I'm just working on getting the basic chat scenario up and running while assigning users to a session. From my research I've realized there are about as many ways to do this as there are stars. I've also realized that there really isnt one "accepted standard" (if I'm wrong, do show me). This was the simplest manner I figured out (using req.session.'name' = 'value')
Heres my server code:
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, path = require('path')
, http = require('http')
, io = require('socket.io')
, cookie = require("cookie")
, connect = require("connect")
, app = express();
// all environments
app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({secret: 'secret'}));
app.use(app.router);
});
var server = http.createServer(app);
server.listen(app.get('port'), function() {
console.log('ready');
});
var io = io.listen(server);
app.get('/', routes.index);
app.get('/user', routes.user);
io.sockets.on('connection', function (socket) {
socket.emit('connected', { connect: 'CONNECTED TO THE SERVER' });
socket.on('client connect', function (data) {
console.log("HEARD THE CLIENT");
});
socket.on('addingNewUser', function (data) {
console.log('BROADCASTING ADDUSER');
console.log(data);
socket.broadcast.emit('addUser', data);
});
});
Heres my index.jade code:
extends layout
block content
script(src="/socket.io/socket.io.js")
script(src="/javascripts/index.js")
script.
var socket = io.connect('http://localhost:3000');
socket.on('connected', function (data) {
socket.emit('client connect', { connect: 'CLIENT IS CONNECTED' });
});
socket.on('addUser', function (data) {
console.log("ADDING NEW USER:");
req.session.user = data;
console.log(data);
$("#usernames").append('<p>'+data+'</p>');
});
$(document).ready(function(){
$('#submit').click( function (data) {
console.log("SUBMITTING FORM!");
var userName = $("#add_user").val();
console.log("user name " + userName);
socket.emit('addingNewUser', { user: userName });
return false;
});
});
#users.large-3.columns
h5 Online Users
p#usernames
#messages.large-6.columns
form
fieldset
legend I am (the) Legend
input(type='text', id='add_user', name='add_user', placeholder='Type your name.')
input.button.small(type='submit', id="submit", value='Send')
#show_messages
ul#user_message
.large-3.columns
Some of the indentation might be off due to the StackOverflow formatting but in the actual file its good.
You can track all the console logs pretty well. It stops at the console.log after "BROADCASTING USER".
For some reason the listener for 'addUser' isnt picking up anything.
Any ideas?
Edit:
As pointed out in the comments, broadcast only emits to the sockets it did not originate from. So thats why it was never triggering the listener.
However, now I have a new problem. One that I've come across before.
In the listener for 'addUser' it returns an error for 'req.session.user' saying that 'Uncaught ReferenceError: req is not defined'.
How do I overcome this?
You are mixing up your client and server logic.
This code (from your template):
socket.on('addUser', function (data) {
console.log("ADDING NEW USER:");
req.session.user = data;
console.log(data);
$("#usernames").append('<p>'+data+'</p>');
});
Is executing on the client side where there is not req object available.
By default, socket.io and express sessions don't play together - you need to add some additional logic to your app.
There are several projects that aim to make this easier such as:
express.io
session.socket.io
I'm trying to create a standalone test suite using mocha, that in a perfect world would start up my express.js application, use zombie to render a page, check a bunch of stuff and then teardown/kill the express.js application.
Is there an easy/best way to do this?
NB. I could just have the express application server running prior to running the tests, but what good are Yaks if you're not going to shave them.
First, you need to move your actual app setting up into a module, and import that into the file that actually starts your app. Now that this is seperate, you can have the app in its complete state before actually listening.
You should move the actual setting up of your app into a separate file, let's call it app.js, can call listen from the file you run node off of, let's call it index.js.
So, app.js would look like:
var express = require('express')
, routes = require('./routes');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
and index.js would look like:
var app = require('./app');
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
This seperates loading of your app from actually having it listen, allowing you to load that app into your unit tests.
In your unit tests, you would do something in a setup method and teardown method to bring up and bring down the server.
In the file test/app_tests.js:
describe('app', function(){
var app = require('../app');
beforeEach(function(){
app.listen(3000);
});
// tests here
afterEach(function(){
app.close();
})
});
In addition to Oved D answer.
Describe your app in express-app.js or some other file:
module.exports = function (o) {
o = o || {};
var app = express();
// app.configure
// configure routes
if (o.someOption) {
// some additional test code
}
return app;
}
describe tests in test/01-some.js:
var expressApp = require('../express-app');
describe('some', function () {
// just describe needed vars
var app, server, port;
// describe setup
before(function (next) {
// create app when
app = expressApp({routes: /api\/some\/.*/ /* put here some test options ...*/});
// creating listener with random port
server = app.listen(function () {
// store port when it ready
port = server.address().port;
// and go to tests
next();
});
});
// tests
it('should return valid result', function (done) {
// do a simple request to /api/some
http.request({
host: 'localhost',
port: port,
path: '/api/some'
}, function (res) {
if (res.err) throw new Error(res.err);
done();
});
});
// teardown
after(function () {
// stop listening that port
server.close();
});
});
Done. ;-)
Now you can create any count of tests like that. Recommend you to enable only needed urls and services in tests with defining it by passing params to express-app.js module.
Update:
Not sure how it works in mocha but better to move before and after calls to init.js and load it with mocha --require init.js.
File should looks like that:
// use it in your mocha tests
global.setupEnv = function setupEnv (o, before, after) {
// just describe needed vars
var env = Object.create(null);
// setup
before(function (next) {
// create app
env.app = expressApp(o);
// creating listener with random port
env.server = env.app.listen(function () {
// store port when it ready
port = env.server.address().port;
env.app.log('Listening on ', env.port);
// and go to tests
next();
});
});
// teardown
after(function () {
// stop listening that port
env.server.close();
});
return env;
}
And in your tests:
// requiring dependencies
var request = require('request');
describe('api', function () {
// describe setup
var env = global.setupEnv({
routes: 'api/some'
}, before, after);
// tests
it('should pass', function (done) {
request('http://localhost:' + env.port, function (error, response, body) {
done();
});
});
});