What is the express.js equivalent of the node.js http module's "req.url"?

MDS

When using vanilla node.js' http.createServer, you can get the url that the client requested with req.url.

When using express.js, the code for responding to a HTTP GET request is something like:

app.get('/', function (req, res) {
  res.send('Hello World!')
})

My problem with this is that it requires you to have an individual app.get for every single page or item on the server. How do I use express.js like the vanilla node.js server to deal with each get request within the callback generated by the http.createServer?

josh3736

You shouldn't ever have a need to use req.url. Express allows you to have variables in your routes.

app.get('/article/:id', function(req, res) {
    // req.params.id will be whatever came after /article/
});

You can also get fancy and use a regular expression.

app.get(/\/article(\d+)/, function(req, res) {
    // req.params[0] will have the digits of /article123
});

If you really want the URL, req.url still exists. However, you should use routes to handle all your routing logic. I've never really had the need to use req.url.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

相当于node.js http模块的“ req.url”的express.js是什么?

来自分类Dev

Express (node.js) using HTTPS and HTTP

来自分类Dev

什么是 express node.js 中的 %s

来自分类Dev

Node JS:Express入门

来自分类Dev

如何使用Express关闭Node.js中的HTTP连接

来自分类Dev

使用HTTPS和HTTP Express(node.js)

来自分类Dev

在Node.js中获取URL的响应(express / http)

来自分类Dev

Angular 2 http发布+ Node.js Express

来自分类Dev

在node.js / express中需要http.createServer(app)

来自分类Dev

Node.js / Express / Jade POST表单404s

来自分类Dev

node.js服务器和带有express.js的HTTP / 2(2.0)

来自分类Dev

从 AngularJS $http/AJAX 到基于 Node.js + Express.js 的 API 的 POST、PUT、DELETE 请求

来自分类Dev

Express JS委托HTTP动词

来自分类Dev

无法下载 Express Node JS

来自分类Dev

node.js(express)链接CSS / JS

来自分类Dev

仅使用Node.js进行异步HTTP(S)遍历

来自分类Dev

websocket与http模块一起使用,但不适用于express(node.js)

来自分类Dev

在Node.js Express日志文件中找到奇怪的http请求

来自分类Dev

在Node.JS和Express中的HTTPS服务器上拦截HTTP响应

来自分类Dev

使用EBS和ELB环境将HTTP转发到Node.js Express应用中的https

来自分类Dev

访问node.js express中的HTTP服务器对象

来自分类Dev

在Node JS中,如何创建端点传递?我正在使用express和http

来自分类Dev

AngularJS $ http请求未在Node JS Express中检测为xhr

来自分类Dev

等待Node.js Express应用在for循环中调用http.request

来自分类Dev

如何在Express / Node JS中发送错误的HTTP响应?

来自分类Dev

Node.JS Express API多行powershell输出到HTTP响应res.send

来自分类Dev

在Node.JS和Express中的HTTPS服务器上拦截HTTP响应

来自分类Dev

使用http查询字符串作为数据库对象node.js / express

来自分类Dev

在Node JS中,如何创建端点传递?我正在使用express和http

Related 相关文章

  1. 1

    相当于node.js http模块的“ req.url”的express.js是什么?

  2. 2

    Express (node.js) using HTTPS and HTTP

  3. 3

    什么是 express node.js 中的 %s

  4. 4

    Node JS:Express入门

  5. 5

    如何使用Express关闭Node.js中的HTTP连接

  6. 6

    使用HTTPS和HTTP Express(node.js)

  7. 7

    在Node.js中获取URL的响应(express / http)

  8. 8

    Angular 2 http发布+ Node.js Express

  9. 9

    在node.js / express中需要http.createServer(app)

  10. 10

    Node.js / Express / Jade POST表单404s

  11. 11

    node.js服务器和带有express.js的HTTP / 2(2.0)

  12. 12

    从 AngularJS $http/AJAX 到基于 Node.js + Express.js 的 API 的 POST、PUT、DELETE 请求

  13. 13

    Express JS委托HTTP动词

  14. 14

    无法下载 Express Node JS

  15. 15

    node.js(express)链接CSS / JS

  16. 16

    仅使用Node.js进行异步HTTP(S)遍历

  17. 17

    websocket与http模块一起使用,但不适用于express(node.js)

  18. 18

    在Node.js Express日志文件中找到奇怪的http请求

  19. 19

    在Node.JS和Express中的HTTPS服务器上拦截HTTP响应

  20. 20

    使用EBS和ELB环境将HTTP转发到Node.js Express应用中的https

  21. 21

    访问node.js express中的HTTP服务器对象

  22. 22

    在Node JS中,如何创建端点传递?我正在使用express和http

  23. 23

    AngularJS $ http请求未在Node JS Express中检测为xhr

  24. 24

    等待Node.js Express应用在for循环中调用http.request

  25. 25

    如何在Express / Node JS中发送错误的HTTP响应?

  26. 26

    Node.JS Express API多行powershell输出到HTTP响应res.send

  27. 27

    在Node.JS和Express中的HTTPS服务器上拦截HTTP响应

  28. 28

    使用http查询字符串作为数据库对象node.js / express

  29. 29

    在Node JS中,如何创建端点传递?我正在使用express和http

热门标签

归档