node.js - Difference between app.use and app.get in express.js -


i'm kind of new express , node.js, , can't figure out difference between app.use , app.get. seems can use both of them send information. example:

app.use('/',function(req, res,next) {     res.send('hello');     next(); }); 

seems same this:

app.get('/', function (req,res) {    res.send('hello'); }); 

app.use() intended binding middleware application. path "mount" or "prefix" path , limits middleware apply paths requested begin it. can used embed application:

// subapp.js var express = require('express'); var app = modules.exports = express(); // ... 
// server.js var express = require('express'); var app = express();  app.use('/subapp', require('./subapp'));  // ... 

by specifying / "mount" path, app.use() respond path starts /, of them , regardless of http verb used:

  • get /
  • put /foo
  • post /foo/bar
  • etc.

app.get(), on other hand, part of express' application routing , intended matching , handling specific route when requested get http verb:

  • get /

and, equivalent routing example of app.use() be:

app.all(/^\/.*/, function (req, res) {     res.send('hello'); }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -