nodejs simple example program of express

The first time you come into contact with nodejs, you can only bite the bullet and learn, let's not talk about it yet, on the code:

const express = require('express')

// 创建服务器
const app = express()

// 监听客户端的请求
// 只有客户端的请求类型是 get,并且 请求地址是 / 根路径的时候,
// 才会调用 后面指定的处理函数
app.get('/', (req, res) => {
  // express 中,封装了更好用的 res.send 方法
  res.send('你好,express 服务器!')
})

// 监听客户端的post请求,并且请求的地址是 /adduser 的时候,
// 才会调用后面指定的处理函数
app.get('/adduser', (req, res) => {
  console.log(req.query);
  res.status(200).json({result:"添加成功"});
})

app.post('/',function(req,res){
	console.log("主页 POST 请求");
	res.send('Hello POST');
})

// 启动服务器
app.listen(4444, () => {
  console.log('express server running at http://127.0.0.1:4444')
})

This is a very simple server, we open the browser and enter: http://127.0.0.1:4444/

 

Then if you enter http://127.0.0.1:4444/adduser

Very easy to understand, look at the code and then see the effect of the actual demonstration. In fact, it is not difficult to see that there are many good examples and explanations on the Internet. I will not explain more here. After all, I am also a novice and I hope to have this. After seeing this, someone with knowledge can give me some comments or suggestions.

Guess you like

Origin blog.csdn.net/smile_5me/article/details/110631699