How to catch a nodejs connect static 404 error? - node.js

I use nodeJS and senchalab's connect middleware (not express!!). I need to catch the static errors (like 404). How do i get these? I tried to check the source, but i could not find a way to pass an error-handler to connect.static.
here is a gist of the general structure i (have to) use:
https://gist.github.com/2397415

Based on your routes:
router.routes['/'] = 'my page';
router.routes['/404'] = '404 test';
I think you meant this:
connect()
.use(connect.static(__dirname + '/public'))
.use(function(req, res, next){
switch (req.url) {
case '/404':
var body = '404 test';
res.statusCode = 404;
res.setHeader('Content-Length', body.length);
res.end(body);
break;
default:
var body = 'my page';
res.setHeader('Content-Length', body.length);
res.end(body);
}
})
.listen(3001);
Also I want to add that in old connect version 1.x you can use 'connect.router', but in 2.x version it was removed and moved to express. If you need useful routing system, use express:
Connect was never meant to be used directly, it exists to support
frameworks like Express

You could add a catch-all routes after static and all your routes:
app.get('*', function(req, res){
//respond with 404 page or something.
})
It will catch all unmatched GET requests.

Related

Nodejs handle unsupported URLs and request types

I would like to add to my web service an option to handle unsupported URLs, I should mention that I'm using Express.
In order to handle bad URLs, (code 404), I tried using
app.use(app.router);
But apparently it's deprecated, what other solutions can I use?
I saw this suggested solution but I would like to hear about other alternatives first.
In addition, my web service support a few HTTP request types, such as GET and POST, how do I properly respond to request types that I do not support? such as DELETE.
The behavior I would like to have is that in case of 404 error, I will return an appropriate response message that's all. Same in case of unsupported requests.
For example:
response.status(404).json({success: false,msg: 'Invalid URL'});
A 404 handler for all unhandled requests in Express would typically look like this:
app.use(function(req, res, next) {
res.status(404).sendFile(localPathToYour404Page);
});
You just make this the last route that you register and it will get called if no other routes have handled the request.
This will also catch methods that you don't support such as DELETE. If you want to customize the response based on what was requested, then you can just put whatever detection and customization code you want inside that above handler.
For example, if you wanted to detect a DELETE request, you could do this:
app.use(function(req, res, next) {
if (req.method === "DELETE") {
res.status(404).sendFile(localPathToYour404DeletePage);
} else {
res.status(404).sendFile(localPathToYour404Page);
}
});
Or, if your response is JSON:
app.use(function(req, res, next) {
let obj = {success: false};
if (req.method === "DELETE") {
obj.msg = "DELETE method not supported";
} else {
obj.msg = "Invalid URL";
}
res.status(404).json(obj);
});
Some references:
Express FAQ: How do I handle 404 responses?
Express Custom Error Pages
And, while you're at it, you should probably put in an Express error handler too:
// note that this has four arguments compared to regular middleware that
// has three arguments
app.use(function (err, req, res, next) {
console.error(err.stack)
res.status(500).send('Something broke!')
});
This allows you to handle the case where any of your middleware encountered an error and called next(err).

Expressjs router based on Content-Type header

For routing, I'd like my middleware to pass the request the routes defined in a /html folder to server HTML(ejs), and if header Content-Type is application/json, use the routes defined in the /api folder.
But I don't want to have to define that in every route.
So I'm not looking for middleware that defines some req.api property that I can check on in every route
app.get('/', function(req, res) {
if(req.api_call) {
// serve api
} else {
// serve html
}
});
But I'd like something like this:
// HTML folder
app.get('/', function(req, res) {
res.send('hi');
});
// API folder
app.get('/', function(req, res) {
res.json({message: 'hi'});
});
Is this possible and if so, how can I do this?
I'd like it to work something like this:
app.use(checkApiCall, apiRouter);
app.use(checkHTMLCall, htmlRouter);
You can insert as the first middleware in the Express chain, a middleware handler that checks the request type and then modifies the req.url into a pseudo URL by adding a prefix path to it. This modification will then force that request to go to only a specific router (a router set up to handle that specific URL prefix). I've verified this works in Express with the following code:
var express = require('express');
var app = express();
app.listen(80);
var routerAPI = express.Router();
var routerHTML = express.Router();
app.use(function(req, res, next) {
// check for some condition related to incoming request type and
// decide how to modify the URL into a pseudo-URL that your routers
// will handle
if (checkAPICall(req)) {
req.url = "/api" + req.url;
} else if (checkHTMLCall(req)) {
req.url = "/html" + req.url;
}
next();
});
app.use("/api", routerAPI);
app.use("/html", routerHTML);
// this router gets hit if checkAPICall() added `/api` to the front
// of the path
routerAPI.get("/", function(req, res) {
res.json({status: "ok"});
});
// this router gets hit if checkHTMLCall() added `/api` to the front
// of the path
routerHTML.get("/", function(req, res) {
res.end("status ok");
});
Note: I did not fill in the code for checkAPICall() or checkHTMLCall() because you were not completely specific about how you wanted those to work. I mocked them up in my own test server to see that the concept works. I assume you can provide the appropriate code for those functions or substitute your own if statement.
Prior Answer
I just verified that you can change req.url in Express middleware so if you have some middleware that modifies the req.url, it will then affect the routing of that request.
// middleware that modifies req.url into a pseudo-URL based on
// the incoming request type so express routing for the pseudo-URLs
// can be used to distinguish requests made to the same path
// but with a different request type
app.use(function(req, res, next) {
// check for some condition related to incoming request type and
// decide how to modify the URL into a pseudo-URL that your routers
// will handle
if (checkAPICall(req)) {
req.url = "/api" + req.url;
} else if (checkHTMLCall(req)) {
req.url = "/html" + req.url;
}
next();
});
// this will get requests sent to "/" with our request type that checkAPICall() looks for
app.get("/api/", function(req, res) {
res.json({status: "ok"});
});
// this will get requests sent to "/" with our request type that checkHTMLCall() looks for
app.get("/html/", function(req, res) {
res.json({status: "ok"});
});
Older Answer
I was able to successfully put a request callback in front of express like this and see that it was succesfully modifying the incoming URL to then affect express routing like this:
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(function(req, res) {
// test modifying the URL before Express sees it
// this could be extended to examine the request type and modify the URL accordingly
req.url = "/api" + req.url;
return app.apply(this, arguments);
});
server.listen(80);
app.get("/api/", function(req, res) {
res.json({status: "ok"});
});
app.get("/html/", function(req, res) {
res.end("status ok");
});
This example (which I tested) just hardwires adding "/api" onto the front of the URL, but you could check the incoming request type yourself and then make the URL modification as appropriate. I have not yet explored whether this could be done entirely within Express.
In this example, when I requested "/", I was given the JSON.
To throw my hat in the ring, I wanted easily readable routes without having .json suffixes everywhere.
router.get("/foo", HTML_ACCEPTED, (req, res) => res.send("<html><h1>baz</h1><p>qux</p></html>"))
router.get("/foo", JSON_ACCEPTED, (req, res) => res.json({foo: "bar"}))
Here's how those middlewares work.
function HTML_ACCEPTED (req, res, next) { return req.accepts("html") ? next() : next("route") }
function JSON_ACCEPTED (req, res, next) { return req.accepts("json") ? next() : next("route") }
Personally I think this is quite readable (and therefore maintainable).
$ curl localhost:5000/foo --header "Accept: text/html"
<html><h1>baz</h1><p>qux</p></html>
$ curl localhost:5000/foo --header "Accept: application/json"
{"foo":"bar"}
Notes:
I recommend putting the HTML routes before the JSON routes because some browsers will accept HTML or JSON, so they'll get whichever route is listed first. I'd expect API users to be capable of understanding and setting the Accept header, but I wouldn't expect that of browser users, so browsers get preference.
The last paragraph in ExpressJS Guide talks about next('route'). In short, next() skips to the next middleware in the same route while next('route') bails out of this route and tries the next one.
Here's the reference on req.accepts.

How can I use middleware alongside express.static?

I have a nodejs application that serves a single page app via express.static. This all works fine, however when I try and create a simple piece of middleware:
app.use(function(req, res, next){
console.log('%s %s', req.method, req.url);
next();
});
app.use(express.static(path.join(__dirname, 'client')));
any attempt to load content from client fails with:
TypeError: Object function (req, res, next){
console.log('%s %s', req.method, req.url);
next();
} has no method 'concat'
If I use the middleware after the express.static call it works fine - but isn't called for static content. I need to setup the middleware so that any flash messages (from connect flash) can be sent as cookies to the static content.
Does anyone know how I can use middleware for all content, including static content? Eventually I'll be serving two folders, one public and one private (authenticated via passport).
I've put together a minimal implementation of your question and it works for me:
var express = require('express')
var path = require('path')
var app = express()
app.use(function(req, res, next) {
console.log('Middleware says %s %s', req.method, req.url);
next();
})
app.use(express.static(path.join(__dirname, 'client')))
app.listen(8080, function() {
console.log('server is ready')
})
I then started the server
$ node so.js
server is ready
and loaded http://localhost:8080/foo.txt in my browser
Middleware says GET /foo.txt
I'm using Express 3.6.0 - if you're using an older version of Express then you may well have stumbled across a bug that's since been fixed, similar to this one. If updating doesn't solve your problem then I would recommend updating your question to contain more code, perhaps a runnable, yet minimal example of the issue. Hope this helps!

AngularJS $resource makes HTTP OPTIONS request instead of HTTP POST for $save method

I'm in the process of writing a simple library application to get ready for a larger project with AngularJS. After reading a lot online about using $resource to interact with a RESTful API, I decided that it would probably offer some time-saving and scaling benefits to implement it instead of using $http for each request. The problem is that for some reason (I'm no expert on CORS and the request is being sent cross-domain) when using the $save method my Node.js console shows:
OPTIONS /books 200 1ms - 161b
Using the query() method works fine - the Node console shows:
GET /books 200 1ms - 228b
I've been stuck for several hours at this point, trying variations on the below but it always ends up being an OPTIONS request instead of POST (which is what it should be according to the Angular documentation) for the $save method.
AngularJS Web App
app.js
var libraryApp = angular.module('libraryApp', ['ngResource', 'ngRoute', 'libraryControllers']);
libraryApp.factory('$book', ['$resource', function ($resource) {
return $resource('http://mywebserver\\:1337/books/:bookId', { bookId: '#bookId' });
}]);
controllers.js
var libraryControllers = angular.module('libraryControllers', []);
libraryControllers.controller('BookCtrl', ['$scope', '$book', function($scope, $book) {
...
$scope.addBook = function () {
var b = new $book;
b.isbn = "TEST";
b.description = "TEST";
b.price = 9.99;
b.$save();
};
}]);
Node.js with Express REST API
app.js
var express = require('express'),
books = require('./routes/books'),
http = require('http'),
path = require('path');
var app = express();
...
// enable cross-domain scripting
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", req.headers.origin);
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
// routing
app.get('/books', books.getAll);
app.get('/books/:isbn', books.get);
// This is what I want to fire with the $save method
app.post('/books', books.add);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
./routes/books.js
...
exports.add = function(req, res) {
console.log("POST request received...");
console.log(req.body.isbn);
};
Tried putting this line in my config function delete $httpProvider.defaults.headers.common["X-Requested-With"]; but no change.
I'm no Angular/Node pro but right now I'm thinking that it's something to do with it being cross domain and, like I said, I'm no expert on CORS.
Thanks in advance.
I know it may be in bad taste to answer my own question but I figured out the problem a few days after posting this.
It all comes down to how browsers manage CORS. When making a cross-domain request in JavaScript that is not "simple" (i.e. a GET request - which explains why the query() function worked), the browser will automatically make a HTTP OPTIONS request to the specified URL/URI, called a "pre-flight" request or "promise". As long as the remote source returns a HTTP status code of 200 and relevant details about what it will accept in the response headers, then the browser will go ahead with the original JavaScript call.
Here's a brief jQuery example:
function makeRequest() {
// browser makes HTTP OPTIONS request to www.myotherwebsite.com/api/test
// and if it receives a HTTP status code of 200 and relevant details about
// what it will accept in HTTP headers, then it will make this POST request...
$.post( "www.myotherwebsite.com/api/test", function(data) {
alert(data);
});
// ...if not then it won't - it's that simple.
}
All I had to do was add the details of what the server will accept in the response headers:
// apply this rule to all requests accessing any URL/URI
app.all('*', function(req, res, next) {
// add details of what is allowed in HTTP request headers to the response headers
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Credentials', false);
res.header('Access-Control-Max-Age', '86400');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
// the next() function continues execution and will move onto the requested URL/URI
next();
});
And then insert these few lines before the Express routing to simply return a HTTP 200 status code for every OPTIONS request:
// fulfils pre-flight/promise request
app.options('*', function(req, res) {
res.send(200);
});
Hopefully this will help anyone who stumbles on this page suffering from the same problem.
I didn´t actually try this, but wouldn´t it be enough to tell the Ressource how to handle the $save request?
$resource('http://mywebserver\\:1337/books/:bookId', { bookId: '#bookId' }, {save: {method: 'POST'});

How to use Node.js 0.8.x domains with express?

How can I create Express/Connect middleware which wrap each request in its own domain?
This set of slides on Speaker Deck gives a succinct overview:
Domains in node 0.8
Express middleware code from the slides:
var createDomain = require('domain').create;
app.use(function(req, res, next) {
var domain = createDomain();
domain.on('error', function(err) {
// alternative: next(err)
res.statusCode = 500;
res.end(err.message + '\n');
domain.dispose();
});
domain.enter();
next();
});
UPDATE: The approach described below has been implemented in the connect-domain NodeJS module, which can be used in either Connect or Express applications.
As of Express 3, express.createServer is deprecated, and its callback should be converted to a middleware. In the middleware, it's important to add the request and result objects to the request domain so that errors fired by them are handled by the domain error handler.
My middleware looks something like this:
var domain = require('domain');
app.use(function(req, res, next) {
var requestDomain = domain.create();
requestDomain.add(req);
requestDomain.add(res);
requestDomain.on('error', next);
requestDomain.run(next);
});
You can avoid adding the request and response to a request domain if you call http.createServer from within a top-level domain, but the Domain docs seem to indicate that per-request domains are a best practice.
Note that the code above doesn't do any domain clean up actions, such as forcibly disposing the request domain. My middleware chooses instead to pass the error through the middleware stack again to be handled by specific error-handling middleware later on. YMMV.
I've had good luck replacing the stock
var app = express.createServer();
with:
var domainCreate = require('domain').create;
var app = express.createServer(function (req, res, next) {
var domain = domainCreate();
domain.run(next);
});
Then in your middleware you can add properties to process.domain or add additional error handling.
This is a late answer, but check out the express-domain-moddleware module. It automatically creates a new domain for each request. The active domain can be referenced by process.domain in your routes. Here is an example:
//with domain-middleware
app.use(require('express-domain-middleware'));
app.use(app.router);
app.use(function errorHandler(err, req, res, next) {
console.log('error on request %d %s %s: %j', process.domain.id, req.method, req.url, err);
res.send(500, "Something bad happened. :(");
if(err.domain) {
//you should think about gracefully stopping & respawning your server
//since an unhandled error might put your application into an unknown state
}
});
app.get('/error', function(req, res, next) {
db.query('SELECT happiness()', process.domain.intercept(function(rows) {
fs.readFile('asldkfjasdf', process.domain.intercept(function(contents) {
process.nextTick(process.domain.intercept(function() {
throw new Error("The individual request will be passed to the express error handler, and your application will keep running.");
}));
}));
}));
});
The domains are currently deprecated in node:
https://nodejs.org/api/domain.html
For the purpose of 'zoning errors', I've created a library which allows you to write asynchronous code in a nice way: https://github.com/vacuumlabs/yacol . One of its benefits is that you can have domains-like behavior with a very nice semantics; check it out!

Resources