What is the difference among app.use(), app.get () , router.get() and router.use() in Express js?
I still don't know the difference between these end points, and the end they are all routes but I don't know when or where should I use it? In what scenario?
app.use('/user/:id', function (req, res, next) {
console.log('Request Type:', req.method)
next()
});
app.get('/user/:id', function (req, res, next) {
res.send('USER')
});
router.get('/user/:id', function (req, res, next) {
res.send('USER')
});
router.use('/user/:id', function (req, res, next) {
res.send('USER')
});
I you guys can help me out with this.
javascript node.js express
add a comment |
I still don't know the difference between these end points, and the end they are all routes but I don't know when or where should I use it? In what scenario?
app.use('/user/:id', function (req, res, next) {
console.log('Request Type:', req.method)
next()
});
app.get('/user/:id', function (req, res, next) {
res.send('USER')
});
router.get('/user/:id', function (req, res, next) {
res.send('USER')
});
router.use('/user/:id', function (req, res, next) {
res.send('USER')
});
I you guys can help me out with this.
javascript node.js express
add a comment |
I still don't know the difference between these end points, and the end they are all routes but I don't know when or where should I use it? In what scenario?
app.use('/user/:id', function (req, res, next) {
console.log('Request Type:', req.method)
next()
});
app.get('/user/:id', function (req, res, next) {
res.send('USER')
});
router.get('/user/:id', function (req, res, next) {
res.send('USER')
});
router.use('/user/:id', function (req, res, next) {
res.send('USER')
});
I you guys can help me out with this.
javascript node.js express
I still don't know the difference between these end points, and the end they are all routes but I don't know when or where should I use it? In what scenario?
app.use('/user/:id', function (req, res, next) {
console.log('Request Type:', req.method)
next()
});
app.get('/user/:id', function (req, res, next) {
res.send('USER')
});
router.get('/user/:id', function (req, res, next) {
res.send('USER')
});
router.use('/user/:id', function (req, res, next) {
res.send('USER')
});
I you guys can help me out with this.
javascript node.js express
javascript node.js express
asked Nov 24 '18 at 6:49
Manfred TijerinoManfred Tijerino
162
162
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
.use() and .get() difference is .use() will listen any type of request and .get() will listen only GET request like .post() .put() etc.
For routers you can think it as a sub route.
For example you can do something like this.
router.get('/:id', (req, res, next) => {res.send('user')})
router.post('/', (req, res, next) => {/* some create user logic */})
app.use('/user', router) // if you do this routers will match '/user/:id' and '/user' path
and of course you can have multiple routers in same app. So your code can be more structured.
add a comment |
By default, .use()
has two main differences with .get()
.
.get()
will only match GET requests,.use()
will match any type of request (POST, PUT, GET, etc...)- If you supply a path to
.use()
, it is more liberal in its matching and it will match if the path "starts" with the path you pass to.use()
. It is done this way so you can set up a middleware handler that will fire for a broad set of URLs, not just a single URL..get()
is more often used with constrained URLs like one specific URL.
app.get()
is pretty much the same as router.get()
. The app
object is a router that also has some other properties and methods on it. So, most any method from a router object is also on the app
object, but not vice versa.
You might use a router instead of the app object for a variety of reasons:
- Code modularity. A module creates it's own router, sets up a bunch of routes on it and then exports the single router which the caller can then add to the current app.
- You have a bunch of routes all with the same prefix and find it cleaner to set up a router for that common prefix and then just put the routes on that router without having to specify the prefix in every one of them.
- To create a set of routes that share the same middleware processing, but other routes in your app do not share that middleware processing. In this case, you create the router, put some middleware on it and then define routes on that router. The middleware will only fire for routes that are routed to this router.
Some examples:
// matches /hello and /hello/hi and /hello/goodbye for GET, POST or PUT
app.use('/hello', ...);
// matches only a GET request for /hello
app.get('/hello', ...);
An example of using a router to help with modularity.
A module with some routes defined in it:
// some_routes.js
const router = require('express').Router();
router.use(someMiddlewareForWholeRouter);
router.get('/hello', ...);
router.get('/goodbye', ...);
router.get('/callme', ...);
module.exports = router;
Using that module in your app:
// app.js
const some_routes = require('./some_routes.js');
// hook up all routes from some_routes with the path prefix of /greeting
app.use('/greetings', some_routes);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455871%2fwhat-is-the-difference-among-app-use-app-get-router-get-and-router-use%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
.use() and .get() difference is .use() will listen any type of request and .get() will listen only GET request like .post() .put() etc.
For routers you can think it as a sub route.
For example you can do something like this.
router.get('/:id', (req, res, next) => {res.send('user')})
router.post('/', (req, res, next) => {/* some create user logic */})
app.use('/user', router) // if you do this routers will match '/user/:id' and '/user' path
and of course you can have multiple routers in same app. So your code can be more structured.
add a comment |
.use() and .get() difference is .use() will listen any type of request and .get() will listen only GET request like .post() .put() etc.
For routers you can think it as a sub route.
For example you can do something like this.
router.get('/:id', (req, res, next) => {res.send('user')})
router.post('/', (req, res, next) => {/* some create user logic */})
app.use('/user', router) // if you do this routers will match '/user/:id' and '/user' path
and of course you can have multiple routers in same app. So your code can be more structured.
add a comment |
.use() and .get() difference is .use() will listen any type of request and .get() will listen only GET request like .post() .put() etc.
For routers you can think it as a sub route.
For example you can do something like this.
router.get('/:id', (req, res, next) => {res.send('user')})
router.post('/', (req, res, next) => {/* some create user logic */})
app.use('/user', router) // if you do this routers will match '/user/:id' and '/user' path
and of course you can have multiple routers in same app. So your code can be more structured.
.use() and .get() difference is .use() will listen any type of request and .get() will listen only GET request like .post() .put() etc.
For routers you can think it as a sub route.
For example you can do something like this.
router.get('/:id', (req, res, next) => {res.send('user')})
router.post('/', (req, res, next) => {/* some create user logic */})
app.use('/user', router) // if you do this routers will match '/user/:id' and '/user' path
and of course you can have multiple routers in same app. So your code can be more structured.
answered Nov 24 '18 at 8:24
Delgee BDelgee B
665
665
add a comment |
add a comment |
By default, .use()
has two main differences with .get()
.
.get()
will only match GET requests,.use()
will match any type of request (POST, PUT, GET, etc...)- If you supply a path to
.use()
, it is more liberal in its matching and it will match if the path "starts" with the path you pass to.use()
. It is done this way so you can set up a middleware handler that will fire for a broad set of URLs, not just a single URL..get()
is more often used with constrained URLs like one specific URL.
app.get()
is pretty much the same as router.get()
. The app
object is a router that also has some other properties and methods on it. So, most any method from a router object is also on the app
object, but not vice versa.
You might use a router instead of the app object for a variety of reasons:
- Code modularity. A module creates it's own router, sets up a bunch of routes on it and then exports the single router which the caller can then add to the current app.
- You have a bunch of routes all with the same prefix and find it cleaner to set up a router for that common prefix and then just put the routes on that router without having to specify the prefix in every one of them.
- To create a set of routes that share the same middleware processing, but other routes in your app do not share that middleware processing. In this case, you create the router, put some middleware on it and then define routes on that router. The middleware will only fire for routes that are routed to this router.
Some examples:
// matches /hello and /hello/hi and /hello/goodbye for GET, POST or PUT
app.use('/hello', ...);
// matches only a GET request for /hello
app.get('/hello', ...);
An example of using a router to help with modularity.
A module with some routes defined in it:
// some_routes.js
const router = require('express').Router();
router.use(someMiddlewareForWholeRouter);
router.get('/hello', ...);
router.get('/goodbye', ...);
router.get('/callme', ...);
module.exports = router;
Using that module in your app:
// app.js
const some_routes = require('./some_routes.js');
// hook up all routes from some_routes with the path prefix of /greeting
app.use('/greetings', some_routes);
add a comment |
By default, .use()
has two main differences with .get()
.
.get()
will only match GET requests,.use()
will match any type of request (POST, PUT, GET, etc...)- If you supply a path to
.use()
, it is more liberal in its matching and it will match if the path "starts" with the path you pass to.use()
. It is done this way so you can set up a middleware handler that will fire for a broad set of URLs, not just a single URL..get()
is more often used with constrained URLs like one specific URL.
app.get()
is pretty much the same as router.get()
. The app
object is a router that also has some other properties and methods on it. So, most any method from a router object is also on the app
object, but not vice versa.
You might use a router instead of the app object for a variety of reasons:
- Code modularity. A module creates it's own router, sets up a bunch of routes on it and then exports the single router which the caller can then add to the current app.
- You have a bunch of routes all with the same prefix and find it cleaner to set up a router for that common prefix and then just put the routes on that router without having to specify the prefix in every one of them.
- To create a set of routes that share the same middleware processing, but other routes in your app do not share that middleware processing. In this case, you create the router, put some middleware on it and then define routes on that router. The middleware will only fire for routes that are routed to this router.
Some examples:
// matches /hello and /hello/hi and /hello/goodbye for GET, POST or PUT
app.use('/hello', ...);
// matches only a GET request for /hello
app.get('/hello', ...);
An example of using a router to help with modularity.
A module with some routes defined in it:
// some_routes.js
const router = require('express').Router();
router.use(someMiddlewareForWholeRouter);
router.get('/hello', ...);
router.get('/goodbye', ...);
router.get('/callme', ...);
module.exports = router;
Using that module in your app:
// app.js
const some_routes = require('./some_routes.js');
// hook up all routes from some_routes with the path prefix of /greeting
app.use('/greetings', some_routes);
add a comment |
By default, .use()
has two main differences with .get()
.
.get()
will only match GET requests,.use()
will match any type of request (POST, PUT, GET, etc...)- If you supply a path to
.use()
, it is more liberal in its matching and it will match if the path "starts" with the path you pass to.use()
. It is done this way so you can set up a middleware handler that will fire for a broad set of URLs, not just a single URL..get()
is more often used with constrained URLs like one specific URL.
app.get()
is pretty much the same as router.get()
. The app
object is a router that also has some other properties and methods on it. So, most any method from a router object is also on the app
object, but not vice versa.
You might use a router instead of the app object for a variety of reasons:
- Code modularity. A module creates it's own router, sets up a bunch of routes on it and then exports the single router which the caller can then add to the current app.
- You have a bunch of routes all with the same prefix and find it cleaner to set up a router for that common prefix and then just put the routes on that router without having to specify the prefix in every one of them.
- To create a set of routes that share the same middleware processing, but other routes in your app do not share that middleware processing. In this case, you create the router, put some middleware on it and then define routes on that router. The middleware will only fire for routes that are routed to this router.
Some examples:
// matches /hello and /hello/hi and /hello/goodbye for GET, POST or PUT
app.use('/hello', ...);
// matches only a GET request for /hello
app.get('/hello', ...);
An example of using a router to help with modularity.
A module with some routes defined in it:
// some_routes.js
const router = require('express').Router();
router.use(someMiddlewareForWholeRouter);
router.get('/hello', ...);
router.get('/goodbye', ...);
router.get('/callme', ...);
module.exports = router;
Using that module in your app:
// app.js
const some_routes = require('./some_routes.js');
// hook up all routes from some_routes with the path prefix of /greeting
app.use('/greetings', some_routes);
By default, .use()
has two main differences with .get()
.
.get()
will only match GET requests,.use()
will match any type of request (POST, PUT, GET, etc...)- If you supply a path to
.use()
, it is more liberal in its matching and it will match if the path "starts" with the path you pass to.use()
. It is done this way so you can set up a middleware handler that will fire for a broad set of URLs, not just a single URL..get()
is more often used with constrained URLs like one specific URL.
app.get()
is pretty much the same as router.get()
. The app
object is a router that also has some other properties and methods on it. So, most any method from a router object is also on the app
object, but not vice versa.
You might use a router instead of the app object for a variety of reasons:
- Code modularity. A module creates it's own router, sets up a bunch of routes on it and then exports the single router which the caller can then add to the current app.
- You have a bunch of routes all with the same prefix and find it cleaner to set up a router for that common prefix and then just put the routes on that router without having to specify the prefix in every one of them.
- To create a set of routes that share the same middleware processing, but other routes in your app do not share that middleware processing. In this case, you create the router, put some middleware on it and then define routes on that router. The middleware will only fire for routes that are routed to this router.
Some examples:
// matches /hello and /hello/hi and /hello/goodbye for GET, POST or PUT
app.use('/hello', ...);
// matches only a GET request for /hello
app.get('/hello', ...);
An example of using a router to help with modularity.
A module with some routes defined in it:
// some_routes.js
const router = require('express').Router();
router.use(someMiddlewareForWholeRouter);
router.get('/hello', ...);
router.get('/goodbye', ...);
router.get('/callme', ...);
module.exports = router;
Using that module in your app:
// app.js
const some_routes = require('./some_routes.js');
// hook up all routes from some_routes with the path prefix of /greeting
app.use('/greetings', some_routes);
edited Nov 24 '18 at 10:20
answered Nov 24 '18 at 10:13
jfriend00jfriend00
431k55555603
431k55555603
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455871%2fwhat-is-the-difference-among-app-use-app-get-router-get-and-router-use%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown