nodejs中實(shí)現(xiàn)修改用戶路由功能
經(jīng)過(guò)前面幾次的學(xué)習(xí),已經(jīng)可以做下小功能,今天要實(shí)現(xiàn)的是修改用戶路由。
一、users_model.js 功能:定義用戶對(duì)象模型
var mongoose=require('mongoose'),
Schema=mongoose.Schema;
var UserSchema=new Schema({
username:{type:String,unique:true},
email:String,
color:String,
hashed_password:String
});
mongoose.model('User',UserSchema);
二、users_controller.js 功能:為Express服務(wù)器實(shí)現(xiàn)修改用戶路由
var crypto=require('crypto');
var mongoose=require('mongoose'),User=mongoose.model('User');
function hashPW(pwd){
return crypto.createHash('sha256').update(pwd).
digest('base64').toString();
}
//實(shí)現(xiàn)更新用戶路由
exports.updateUser=function(req,res){
User.findOne({_id:req.session.user})
.exec(function(err,user){
user.set('email',req.body.email);
user.set('color',req.body.color);
user.save(function(err){
if(err){
res.sessor.error=err;
}else{
req.session.msg='User Updated. ';
}
res.redirect('/user');
});
});
};
三、user.html
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>用戶修改</title>
<link rel="stylesheet"type="text/css" href="/static/css/styles.css" />
<script src="https://cdn.staticfile.org/angular.js/1.5.8/angular.min.js"></script>
<script src="/static/js/my_app.js"></script>
</head>
<body>
<div class="form-container" ng-controller="myController">
<p class="form-header">用戶修改</p>
<form method="POST" action="/user/update">
<label>用戶名:</label>
<input type="text" name="username" ng-model="user.username" disabled><br>
<label>Email:</label>
<input type="email" name="email" ng-model="user.email"><br>
<label>膚色:</label>
<input type="text" name="color" ng-model="user.color"><br>
<input type="submit" value="Save">
</form>
</div>
<form method="POST" action="/user/delete">
<input type="submit" value="刪除用戶">
</form>
<hr><%=msg%>
<hr>{{error}}
</body>
</html>
四、routes.js 功能:為Express服務(wù)器實(shí)現(xiàn)處理Web請(qǐng)求路由
var crypto=require('crypto');
var express=require('express');
module.exports=function(app){
var users=require('./controllers/users_controller');
app.use('/static',express.static('./static')).
use('/lib',express.static('../lib')
);
app.post('/user/update',users.updateUser);
五、頁(yè)面顯示效果

總結(jié)
以上所述是小編給大家介紹的nodejs中實(shí)現(xiàn)修改用戶路由功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- JS實(shí)現(xiàn)前端路由功能示例【原生路由】
- nuxt.js 在middleware(中間件)中實(shí)現(xiàn)路由鑒權(quán)操作
- Nodejs封裝類(lèi)似express框架的路由實(shí)例詳解
- 關(guān)于vue路由緩存清除在main.js中的設(shè)置
- vue.js路由mode配置之去掉url上默認(rèn)的#方法
- Vue.js路由實(shí)現(xiàn)選項(xiàng)卡簡(jiǎn)單實(shí)例
- Node.js 路由的實(shí)現(xiàn)方法
- 初學(xué)node.js中實(shí)現(xiàn)刪除用戶路由
- 如何制作自己的原生JavaScript路由
相關(guān)文章
nodejs搭建本地服務(wù)器并訪問(wèn)文件操作示例
這篇文章主要介紹了nodejs搭建本地服務(wù)器并訪問(wèn)文件操作,結(jié)合實(shí)例形式分析了nodejs搭建本地服務(wù)器操作步驟及文件訪問(wèn)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05
node thread.sleep實(shí)現(xiàn)示例
這篇文章主要介紹了node thread.sleep實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
nodejs實(shí)現(xiàn)一個(gè)自定義的require方法的詳細(xì)流程
大家對(duì)nodejs中的require方法應(yīng)該不會(huì)陌生,這個(gè)方法可以用來(lái)導(dǎo)入nodejs的內(nèi)置模塊,自定義模塊,第三方模塊等,使用頻率非常高,那么這個(gè)方法內(nèi)部是如何實(shí)現(xiàn)的呢?本篇文章就是從頭到尾拆分實(shí)現(xiàn)流程,最終實(shí)現(xiàn)一個(gè)自定義的require方法,需要的朋友可以參考下2025-03-03
TypeScript開(kāi)發(fā)Node.js程序的方法
這篇文章主要介紹了TypeScript開(kāi)發(fā)Node.js程序的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

