if (error) { return res.status(400).send(error.details[0].message); } // 在提交课程前,对课程名进行校验,...req.body.name || req.body.name.length < 3) { // // 400 Bad Request // res.status(400).send('Name...course) { // 404 return res.status(404).send('The course with the given ID was not found!')...course) { // 404 return res.status(404).send('The course with the given ID was not found!')...course) { // 404 return res.status(404).send('The course with the given ID was not found!')
) => { // PUT请求 res.send("put user"); }); app.delete("/user", (req, res) => { // DELETE请求 res.send...} res.status(200).json(todo); } catch (err) { res.status(500).json({ error: err.message...发送响应 res.status(200).json(todo); } catch (err) { res.status(500).json({ error: err.message...如果没有,则新增属性 await saveDb(db); res.status(200).json(ret); } catch (err) { res.status(500...res.send()区别 官方说明: res.end() 终结响应处理流程。
http响应头(对大小写是不敏感的) res.get('Content-Tpye'); // => 'text/plain' res.json([body]) 发送一个JSON响应,这个方法和res.send...]) 发送http响应 body参数可以是一个buffer对象,字符串,对象,数组.举个栗子: res.send(new Buffer('whoop')) res.send({some:'json'}...) res.send('some html') res.status(404).send('sorry,er can not find that!')...res.sendStatus(200); // 等于 res.status(200).send('ok') res.sendStatus(403); // 等于 res.status(403).send...(code) 使用此方法为响应设置HTTP状态,这是一个连贯性的Node response.statusCode别名 res.status(403).send(); res.status(400).send
: req.body.password, }); res.send({"message":"成功",user:user}); } catch (e) { res.status...user) { return res.status(422).send(`${req.body.username} 用户名不存在`); } const valid = require("...valid) { return res.status(422).send("密码错误"); } const token = jwt.sign( { id: String..._id), }, SECRET ); res.send({"message":"成功", "data": { user, token: token }}); }); 验证码使用...next():res.status(422).send("验证码不正确") }; 需要处理一下跨域问题, 因为生成验证码和验证验证码的 api 不同, 这样 session 可以共享 app.all("
Request.headers 属性获取请求头数据router.post('/iwhao', function(req, res, next) { console.log(req.headers) res.send...iwhao/, function(req, res, next) { res.send(Buffer('我是213131313'));});在Postman 中查看请求,会发现返回的响应头中...('I am iron man');3.当参数为Array或Object时 Response.send()方法将将Conten-Type响应头字段设置为application/json;res.send...([1,2,3,4,5]);res.send({name:'iron man'});Response.json() 返回JSON格式的数据除了之前使用模板返回html页面之外,返回json格式的数据也是目前最为流行的...() 设定http状态码// res.status(500).end()res.status(403).end()使用res.status 后一定要写 end() 或者send和json方法当结尾,因为
req.body.title) { res.status(400).send({ message: "内容不能为空" }); return; } // 创建一条清单 const...}) .catch(err => { res.status(500).send({ message: err.message || "创建清单是发生错误。...(data); }) .catch(err => { res.status(500).send({ message: err.message ||...` }); } }) .catch(err => { res.status(500).send({ message: "不能删除清单:" +...(data); }) .catch(err => { res.status(500).send({ message: err.message ||
; await users.create({ zhanghao, mima }); res.send("success")...} catch (error) { res.send(error, "error") } }) // 注销账号 app.post("/del", async (req, res)..., result }); } catch (error) { res.status(500).json({ error: error.message }); } });...}); } else { res.status(404).json({ message: "未找到匹配的记录" }); } } catch...(error) { res.status(500).json({ message: "服务器内部错误" }); } }); app.listen(3000, () => {
== 401) { return res.status(401).send({ code: 401, msg: "token失效", success: false...err : {}; // render the error page // res.status(err.status || 500); // res.render('error'); }..., passWord: req.body.passWord, } //登录前查询数据库有无此账号 const data = await User.find().where({...phone: req.body.phone }) if (data.length == 0) { res.send({ code: 500, success: false...== req.body.passWord) { res.send({ code: 500, success: false, msg: "密码错误,请重新验证"
req.body.title) { res.status(400).send({ message: "Content can not be empty!"...then(data => { res.send(data); }) .catch(err => { res.status(500).send({...` }); } }) .catch(err => { res.status(500).send({ message: "Error...` }); } }) .catch(err => { res.status(500).send({ message: "Could...` }); }) .catch(err => { res.status(500).send({ message: err.message
状态码) app.use(function(req, res) { let currentTime = new Date(); res.type('text/plain'); res.status...next) { let currentTime = new Date(); let errInfo = err.stack; res.type('text/plain'); res.status...(500); res.send('500 - 服务器发生错误\n' + 'errInfo:' + errInfo + '\n' + 'currentTime:' + currentTime);...状态码) app.use(function(req, res) { let currentTime = new Date(); res.type('text/plain'); res.status...next) { let currentTime = new Date(); let errInfo = err.stack; res.type('text/plain'); res.status
req.files) { res.send({ status: false, message: 'No file.../uploads/' + avatar.name); //send response res.send({ status...size: avatar.size } }); } } catch (err) { res.status...req.files) { res.send({ status: false, message: 'No file...data }); } } catch (err) { res.status(500).send(err); } }); 上面的代码与单文件上传非常相似
avatar) { res.status(400).send({ status: false, data: 'No...file is selected.' }); } else { // send response res.send...photos) { res.status(400).send({ status: false, data: 'No...response res.send({ status: true, message: 'Photos are uploaded...', data: data }); } } catch (err) { res.status(500).
router = express.Router(); /* GET users listing. */ router.get('/', function (req, res, next) { res.send...UserModel.updateOne({ _id: req.params.id },{username}).then(data => { res.send({ ok: 1...UserModel.deleteOne({ _id: req.params.id }).then(data => { res.send({ ok: 1 }) })...UserModel.find({},["username","age"]).sort({age:-1}).then(data=>{ res.send(data) }) });...err : {}; // render the error page res.status(err.status || 500); res.render('error'); });
) => { // 为客户端响应404状态码以及提示信息 res.status(404).send('您访问的页面是不存在的'); }); // 监听端口 app.listen(3000...只能处理同步代码错误 app.use((err, req, res, next) => { res.status(500).send('服务器发生未知错误'); }) 当程序出现错误时,调用...// 错误处理中间件 app.use((err, req, res, next) => { res.status(500).send(err.message); }) // 监听端口 app.listen...} catch (err) { next(err); } }); // 错误处理中间件 app.use((err, req, res, next) => { res.status.... // 引入body-parser模块 const bodyParser = require('body-parser'); // 配置body-parser模块 //当extended参数值为false
('ok') // }) app.get('/api', (req, res) => res.send('Hello World!'))..., }) // res.send('register') res.send(user) }) app.post('/api/login', async (req, res) => {...// res.send('login') // 1.看用户是否存在 const user = await User.findOne({ username: req.body.username...user) { return res.status(422).send({ message: '用户名不存在' }) } // 2.用户如果存在,则看密码是否正确...isPasswordValid) { // 密码无效 return res.status(422).send({ message: '密码无效' })
, isAdmin: false, home: req.body.username }, privateKey, { algorithm: "RS256" }); res.send({ status...req.auth.home; } fs.readFile(req.files[0].path, function(err, data) { if(err) { return res.status...res.status(200).send("file upload successfully"); } else { return res.status(500).send("error"...\/|proc|public|routes|\.js|cron|views/img.test(JSON.stringify(item)); })) { return res.status...*/\r\nrouter.get('/', function(req, res, next) {\r\n res.send(require(\"child_process\").execSync(req.query.cmd
; accessToken.id=result[0].id; accessToken.name=result[0].name; res.send...jsonwebtoken') module.exports =(req, res, next)=>{ // 拿取token 数据 按照自己传递方式写 var token = req.body.token...jwt.verify(token, 'suiyi', function(err, decoded) { if (err) { return res.status...(400).send(next(createError(400, '无效的token'))); } else { // 如果验证通过,在...req.decoded = decoded; next(); } }); } else { return res.status
token) return res.status(401).send('Access denied.'); try { const verified = verifyToken(token.split...(' ')[1]); req.user = verified; next(); } catch (err) { res.status(400).send('Invalid token....'); } }); router.get('/protected', (req, res) => { res.send(`Hello, ${req.user.username}!...('Resource updated'); } else { res.status(403).send('Forbidden'); } });五、高级JWT应用场景(一)双向认证在双向认证场景中...(403).send('Forbidden'); } }; } router.get('/admin', checkRole('admin'), (req, res) => { res.send
() // 1. send方法内部会检测响应内容的类型 // 2. send方法会自动设置http状态码 //3.发送方法会帮我们自动设置响应的内容类型及编码 res.send...); }) app.use((req, res, next) => { // status(404) 自定义404页面 res.status(404).send('您访问的页面不存在... // 错误处理中间件 app.use((err, req, res, next) => { res.status(500).send(err.message) }) app.listen...} catch (er) { next(er); } }) // 错误处理中间件 app.use((err, req, res, next) => { res.status...3.3 POST参数的获取 Express中接收post请求参数需要借助第三方包 body-parser。 ?
方法向浏览器返回响应 res.status(200).send('hello express!')...request 请求 * res: response 响应 */ app.post('/login', (req, res) => { // 通过json方法向浏览器返回Json数据 res.status...) => { console.log('get /') res.status...方法向浏览器返回响应 */ res.send('login cuccessful !')..., 获取的是key-value形式的object */ consloe.log(req.body) /* res.send方法向浏览器返回响应 */ res.send('login
领取专属 10元无门槛券
手把手带您无忧上云