node.js Basic routing

routing:就是响应客户的方法(对应http 的方法)

Basic routing

Routing :一个应用决定如何响应特定终端一个客户请求,特定终端一般是浏览器,输入 URI (or 路径) ,并且对应HTTP 请求方法(GET, POST, and so on).

每一个路由能有一个或更多的处理函数,当路由匹配时执行.

路由定义有如下结构:

app.METHOD(PATH, HANDLER)

Where:

  • app 是express的一个实例.
  • METHOD is an HTTP 请求的方法, 小写.
  • PATH 是浏览器输入的地址.
  • HANDLER 是一个当路由匹配时执行的函数。.

This tutorial assumes that an instance of express named app is created and the server is running. 

The following examples illustrate defining simple routes.

   在主页上,回复一个 Hello World! 

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

在主页上根目录回复一个POST 请求:

app.post('/', function (req, res) {
  res.send('Got a POST request')
})

在主页上/user目录回复一个PUT请求:

Respond to a PUT request to the /user route:

app.put('/user', function (req, res) {
  res.send('Got a PUT request at /user')
})

Respond to a DELETE request to the /user route:

app.delete('/user', function (req, res) {
  res.send('Got a DELETE request at /user')
})



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值