I'm getting a Node/Express API ready for production however have some questions/advice need help on:
1 - I'm using the npm package cors - did so for development to basically disable CORS, however now that I'm moving to production I only want certain URLS to be able to reach the API.
So to so this what I've seen is:
var corsOptions = {
origin: 'http://www.example.com',
methods: ['GET', 'POST']
};
app.get('*', cors(corsOptions), function(req, res, next) {
res.json({msg: 'This is CORS-enabled for only example.com.'});
});
However (from what I've seen) I'd need to do this for everyone request type e.g.
app.post('*', cors(corsOptions), function(req, res, next) {
res.json({msg: 'This is CORS-enabled for only example.com.'});
});
app.del('*', cors(corsOptions), function(req, res, next) {
res.json({msg: 'This is CORS-enabled for only example.com.'});
});
Is there an easier way to avoid such duplication?
2 - And the second question - I have a long list of API routes e.g.
router.post('/registerUser', function(req, res) {
...
}
Is it possible to place some kind of 'otherwise' line so that if a route doesn't match anything I've defined i can return something specific?
Thanks.
For first put cors as a middleware before your first route.
app.use(cors(corsOptions));
//routes down below
Explanation: Cors will be set for all the routes below for every request. As this middleware will be executed first.
For second put your otherwise or 404 route after your last route.
app.use((req,res,next)=>{
//Your message
res.send('Page Not Found');
});
Explanation: If any route will not be matched then this route will trigger.
Related
I am a tyro in node and express, learning the things. I was reading Express's documentation at this link: https://expressjs.com/en/guide/routing.html and in the Route Handlers section of this page, it says:
You can provide multiple callback functions that behave like middleware to handle a request. The only exception is that these callbacks might invoke next('route') to bypass the remaining route callbacks.
The examples given on this page doesn't contain any next() with arguments in it.
I tried implementing it and passing some path as an argument in this function but it behaves strangely and prints that argument in the console and also sends it to the browser.
Below is my code:
const express = require('express');
const port = 8000;
const app = express();
app.get('/', (req, res, next) => {
console.log("I am the first one");
next('/demo');
}, function(req, res, next){
console.log("I am the second one");
});
app.get('/demo', (req, res) => {
console.log("Good!");
});
app.listen(port, (err) => {
if(err){
console.log("ERROS: ",err);
}
console.log("Express server is runnig on port: ",port);
});
In the browser, I typed http://localhost:8000/The output in console is:
I am the first one
/demo
the next('/demo') function call doesn't cause /demo route handler to run.
Where am I going wrong?
Am I getting it right?
Actually I am not able to understand how this argument thing works with the next() function.
Since the documentation page does not have any example with arguments in function, Can anyone please explain how this next() function work with arguments through an example?
Thanks in advance for any help you are able to provide
I did some deep diving here because it seemed interesting to me and then I realized it's just the error being returned. Let me explain.
Basically next() which is a expressjs specific functionality is used to pass over the control to next functional unit. It doesn't expect parameters of string, barring one (given below - after the error piece). That even in a case when you initialize it properly.
Here is the output in the network tab once you load the page, please check.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>/demo</pre>
</body>
</html>
NOTE: next('route') will work only in middleware functions that were
loaded by using the app.METHOD() or router.METHOD() functions.
You cannot call another router the way you are doing at the moment, http will have one response for one request to close the channel. However you can do something like.
app.get('/', (req, res, next) => {
console.log("I am the first one");
next();
}, function(req, res, next){
console.log("I am the second one");
next();
});
app.get('/', (req, res, next) => {
console.log("I am the third one");
next();
}, function(req, res, next){
console.log("I am the fourth one");
});
Here middleware works it's magic and matches whatever identical route parameter is and goes on to execute it all.
The next() function in
app.get('/', (req,...
used to call the next callback in the same route
...
function(req, res, next){
console.log("I am the second one");
});
Once you remove the /demo from the next('/demo'), you will notice "I am the second one" in the console. NOTE: next() only passes the control to the next middleware in the same route.
app.get('/', (req, res, next)=>{
// E.g. purpose of this middleware is to print request method
console.log('Request method:', req.method)
next() // this code passes the control to the next callback which is below
}, (req, res) => {
console.log('End of route')
res.send('OK')
}
It does not engage the app's route. In order to go to another route, you have to send another HTTP request. Hope this helps!
UPDATE Here's the scenario where next('route') does something, this only works if 2 routes are of the same URI and http method.
const express = require('express')
const app = express();
function middleware1(req, res, next){
console.log('Middleware #1')
next('route') // ends current callback chain, moves to the next route
}
app.get('/', middleware1, (req, res, next)=>{
console.log('Second middleware') // this callback is not executed
res.send('OK')
})
app.get('/', (req, res)=>{
console.log('Test') // the callback in this route gets called
res.send('2nd get / OK')
})
app.listen(3000, () => {
console.log('Server #', 3000)
})
I think this implementation of route is seldom being put into practice because personally I feel that it is tedious to follow (having 2 routes with the same uri and http method). Nonetheless it is good to learn something from Gandalf the White and this.
I want to log all requests like this:
8:04:20 PM - info: /api/v2 200
8:04:22 PM - info: /api/v2/asdf 200
However, in express, the middleware is called before the request is processed, so I cannot get the real response code. I always get 200. What is the right approach to accomplish this?
You can use morgan to log your requests:
const morgan = require("morgan");
app.use(morgan('dev'));
For more documentation visit morgan. Yo may also be interested in
on-finished package to execute arbitrary code on request completion.
Here you go:
app.use((req, res, next)=> {
console.log('I run on every request!');
next();
})
Have your middleware below your routes and in your routes add a third parameter on the callback like this:
app.get( "/", function( req, res, next ) {
res.send(something);
next();
});
app.use(function(req, res, next) {
console.log('after request is done');
});
I want to create a middleware function in express.js. which can monitor every requests and responses. I created a middleware but it can only monitor the requests, not the responses.
function middlewareFunc (req,res,next) {
console.log(req.body , req.params , req.query);
next();
}
You should know that res in function(req, res, next) is a instance of class http.ServerResponse. So it can be listen on finish event, please see the link: https://nodejs.org/api/stream.html#stream_event_finish
app.use(function (req, res, next) {
function afterResponse() {
res.removeListener('finish', afterRequest);
res.removeListener('close', afterRequest);
// action after response
}
res.on('finish', afterResponse);
res.on('close', afterResponse);
// action before request
// eventually calling `next()`
});
app.use(app.router);
app.use() and middleware can be used for "before" and a combination of the close and finish events can be used for "after."
For that you can write two middlewares
1) Before all request endpoints.
//middleware
function middlewareFunEarlier(req,res,next) {
console.log(req.body , req.params , req.query);
next();
}
app.use(middlewareFunEarlier);
app.get('/', function(req, res, next){
//do something
res.end();
});
2) After all end points. And you must have to use next() in all endpoints
app.get('/', function(req, res, next){
//do something
next();
});
app.use(middlewareFunLater);
//middlware
function middlewareFunLater(req, res, next){
console.log(res);
res.end();
}
It can be work around with existing tools.
Ok, so first of all, the reason you are only seeing the requests is because of how middleware works. Everything gets run once in a certain order, and runs only once. When your middleware gets run it is most likely before the response has been created. In order to get the response you would have to make your code run when your controller goes to render or something like that.
Second of all, it seems like basic logging is all you need.(weather it be with a library or just console logging stuff.)
I need an equivalent of following express.js code in simple node.js that I can use in middleware. I need to place some checks depending on the url and want to do it in a custom middleware.
app.get "/api/users/:username", (req,res) ->
req.params.username
I have the following code so far,
app.use (req,res,next)->
if url.parse(req.url,true).pathname is '/api/users/:username' #this wont be true as in the link there will be a actual username not ":username"
#my custom check that I want to apply
A trick would be to use this:
app.all '/api/users/:username', (req, res, next) ->
// your custom code here
next();
// followed by any other routes with the same patterns
app.get '/api/users/:username', (req,res) ->
...
If you only want to match GET requests, use app.get instead of app.all.
Or, if you only want to use the middleware on certain specific routes, you can use this (in JS this time):
var mySpecialMiddleware = function(req, res, next) {
// your check
next();
};
app.get('/api/users/:username', mySpecialMiddleware, function(req, res) {
...
});
EDIT another solution:
var mySpecialRoute = new express.Route('', '/api/users/:username');
app.use(function(req, res, next) {
if (mySpecialRoute.match(req.path)) {
// request matches your special route pattern
}
next();
});
But I don't see how this beats using app.all() as 'middleware'.
You can use node-js url-pattern module.
Make pattern:
var pattern = new UrlPattern('/stack/post(/:postId)');
Match pattern against url path:
pattern.match('/stack/post/22'); //{postId:'22'}
pattern.match('/stack/post/abc'); //{postId:'abc'}
pattern.match('/stack/post'); //{}
pattern.match('/stack/stack'); //null
For more information, see: https://www.npmjs.com/package/url-pattern
Just use the request and response objects as you would in a route handler for middleware, except call next() if you actually want the request to continue in the middleware stack.
app.use(function(req, res, next) {
if (req.path === '/path') {
// pass the request to routes
return next();
}
// you can redirect the request
res.redirect('/other/page');
// or change the route handler
req.url = '/new/path';
req.originalUrl // this stays the same even if URL is changed
});
I'm using Node.js with express and already know the existence of response.redirect().
However, I'm looking for more of a forward() functionality similar to java that takes the same parameters as redirect, but internally forwards the request instead of having the client perform the redirect.
To clarify, I am not doing a proxy to a different server. I'd like to forward('/other/path') directly within the same app instance
It wasn't apparently obvious how to do this from the express documentation. Any help?
You just need to invoke the corresponding route handler function.
Option 1: route multiple paths to the same handler function
function getDogs(req, res, next) {
//...
}}
app.get('/dogs', getDogs);
app.get('/canines', getDogs);
Option 2: Invoke a separate handler function manually/conditionally
app.get('/canines', function (req, res, next) {
if (something) {
//process one way
} else {
//do a manual "forward"
getDogs(req, res, next);
}
});
Option 3: call next('route')
If you carefully order your router patterns, you can call next('route'), which may achieve what you want. It basically says to express 'keep moving on down the router pattern list', instead of a call to next(), which says to express 'move down the middleware list (past the router)`.
You can implement forward (aka rewrite) functionality by changing request url property and calling next('route').
Note that the handler performing forward needs to be configured before other routes which you perform forwards to.
This is example of forwarding all *.html documents to routes without .html extension (suffix).
function forwards(req, res, next) {
if (/(?:.+?)\.html$/.test(req.url)) {
req.url = req.url.replace(/\.html$/, '');
}
next('route');
}
You call next('route') as the last operation. The next('route') passes control to subsequent routes.
As mentioned above, you need to configure forwards handler as one of the first handlers.
app.get('*', forwards);
// ...
app.get('/someroute', handler);
The above example will return the same content for /someroute as well as /someroute.html. You could also provide an object with a set of forward rules ({ '/path1': '/newpath1', '/path2': '/newpath2' }) and use them in forward mechanism.
Note that regular expression used in forwards function is simplified for mechanism presentation purposes. You would need to extend it (or perform check on req.path) if you would like to use querystring parameters etc.
I hope that will help.
For Express 4+
Using the next function does not work if the next handler is not added in the right order. Instead of using next, I use the router to register the handlers and call
app.get("/a/path", function(req, res){
req.url = "/another/path";
app.handle(req, res);
}
Or for HTML5 mode of React/Angular
const dir = process.env.DIR || './build';
// Configure http server
let app = express();
app.use('/', express.static(dir));
// This route sends a 404 when looking for a missing file (ie a URL with a dot in it)
app.all('/*\.*', function (req, res) {
res.status(404).send('404 Not found');
});
// This route deals enables HTML5Mode by forwarding "missing" links to the index.html
app.all('/**', function (req, res) {
req.url = 'index.html';
app.handle(req, res);
});
Using the next function does not work if the next handler is not added in the right order. Instead of using next, I use the router to register the handlers and call
router.get("/a/path", function(req, res){
req.url = "/another/path";
router.handle(req, res);
}
Express 4+ with nested routers
Instead of having to use the outside of route/function app, you can use req.app.handle
"use strict";
const express = require("express");
const app = express();
//
// Nested Router 1
//
const routerOne = express.Router();
// /one/base
routerOne.get("/base", function (req, res, next) {
res.send("/one/base");
});
// This routes to same router (uses same req.baseUrl)
// /one/redirect-within-router -> /one/base
routerOne.get("/redirect-within-router", function (req, res, next) {
req.url = "/base";
next();
});
// This routes to same router (uses same req.baseUrl)
// /one/redirect-not-found -> /one/two/base (404: Not Found)
routerOne.get("/redirect-not-found", function (req, res, next) {
req.url = "/two/base";
next();
});
// Using the full URL
// /one/redirect-within-app -> /two/base
routerOne.get("/redirect-within-app", function (req, res, next) {
req.url = "/two/base";
// same as req.url = "/one/base";
//req.url = req.baseUrl + "/base";
req.app.handle(req, res);
});
// Using the full URL
// /one/redirect-app-base -> /base
routerOne.get("/redirect-app-base", function (req, res, next) {
req.url = "/base";
req.app.handle(req, res);
});
//
// Nested Router 2
//
const routerTwo = express.Router();
// /two/base
routerTwo.get("/base", function (req, res, next) {
res.send("/two/base");
});
// /base
app.get("/base", function (req, res, next) {
res.send("/base");
});
//
// Mount Routers
//
app.use("/one/", routerOne);
app.use("/two/", routerTwo);
// 404: Not found
app.all("*", function (req, res, next) {
res.status(404).send("404: Not Found");
});
app.get('/menzi', function (req, res, next) {
console.log('menzi2');
req.url = '/menzi/html/menzi.html';
// res.redirect('/menzi/html/menzi.html');
next();
});
This is my code:when user enter "/menzi",the server will give the page /menzi/html/menzi.html to user, but the url in the browser will not change;
You can use run-middleware module exactly for that. Just run the handler you want by using the URL & method & data.
https://www.npmjs.com/package/run-middleware
For example:
app.runMiddleware('/get-user/20',function(code,body,headers){
res.status(code).send(body)
})