使用ajax发送get请求
var xhr = new XMLHttpRequest()
xhr.open('get', 'http://localhost:3000/first')
xhr.send()
xhr.onload = function() {
console.log(xhr.responseText)
}
新建app.js文件
npm install express -s 下载express模块
const express = require('express')
const path = require('path')
let app = express()
//静态资源托管
app.use(express.static(path.join(__dirname, 'public')))
app.get('/first', (req, res) => {
res.send('hello ajax')
})
app.listen(3000)
console.log('服务器启动成功')
打开浏览器端口,即可看到后端响应数据