node.js實(shí)現(xiàn)學(xué)生檔案管理
本文實(shí)例為大家分享了node.js實(shí)現(xiàn)學(xué)生檔案管理的具體代碼,供大家參考,具體內(nèi)容如下
學(xué)生檔案管理
目標(biāo):模板引擎應(yīng)用,強(qiáng)化node.js項(xiàng)目制作流程
知識(shí)點(diǎn):http請(qǐng)求響應(yīng)、數(shù)據(jù)庫(kù)、模板引擎、靜態(tài)資源訪問(wèn)
制作流程
1、建立項(xiàng)目文件夾并生成項(xiàng)目描述文件
2、創(chuàng)建網(wǎng)站服務(wù)器實(shí)現(xiàn)客戶端和服務(wù)器端通信
3、連接數(shù)據(jù)庫(kù)并根據(jù)需求設(shè)計(jì)學(xué)員信息表
4、創(chuàng)建路由并實(shí)現(xiàn)頁(yè)面模板呈遞
5、實(shí)現(xiàn)靜態(tài)資源訪問(wèn)
6、實(shí)現(xiàn)學(xué)生信息添加功能
1).在模板的表單中指定請(qǐng)求地址與請(qǐng)求方式
2).為每一個(gè)表單項(xiàng)添加name屬性
3).添加實(shí)現(xiàn)學(xué)生信息功能路由
4).接收客戶端傳遞過(guò)來(lái)的學(xué)生信息
5).將學(xué)生信息添加到數(shù)據(jù)庫(kù)中
6).將頁(yè)面重定向到學(xué)生信息列表頁(yè)面
7、實(shí)現(xiàn)學(xué)生信息展示功能
1).從數(shù)據(jù)庫(kù)中將所有的學(xué)生信息查詢出來(lái)
2).通過(guò)模板引擎將學(xué)生信忠和HTML模板進(jìn)行拼接
3).將拼接好的HTML模板響應(yīng)給客戶端
目錄結(jié)構(gòu)
connect.js
const mongoose = require('mongoose'); // 連接數(shù)據(jù)庫(kù) mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true}) .then(() => console.log('連接數(shù)據(jù)庫(kù)成功') ).catch(() => console.log('連接數(shù)據(jù)庫(kù)失敗'))
user.js
const mongoose = require('mongoose'); // 創(chuàng)建學(xué)生集合規(guī)則 const studentsSchema = new mongoose.Schema({ ? ? name: { ? ? ? ? type: String, ? ? ? ? required: true, ? ? ? ? minlength: 2, ? ? ? ? maxlength: 10 ? ? }, ? ? age: { ? ? ? ? type: Number, ? ? ? ? min: 10, ? ? ? ? max: 25 ? ? }, ? ? sex: { ? ? ? ? type: String ? ? }, ? ? email: String, ? ? hobbies: [String], ? ? collage: String, ? ? enterDate: { ? ? ? ? type: Date, ? ? ? ? default: Date.now ? ? } }); // 創(chuàng)建學(xué)生學(xué)習(xí)集合 const Student = mongoose.model('Student',studentsSchema); // 將學(xué)生學(xué)習(xí)集合進(jìn)行導(dǎo)出 module.exports = Student;
list.css
body { ?? ?padding: 0; ?? ?margin: 0; } table { ?? ?border-collapse: collapse; } table, td, th { ?? ?text-align: center; ?? ?line-height: 30px; ?? ?border: 1px solid #CCC; } caption { ?? ?font-weight: bold; ?? ?font-size: 24px; ?? ?margin-bottom: 10px; } table { ?? ?width: 960px; ?? ?margin: 50px auto; } a { ?? ?text-decoration: none; ?? ?color: #333; } a:hover { ?? ?text-decoration: underline; ?? ?color: #000; }
main.css
body { ?? ?margin: 0; ?? ?padding: 0 0 40px; ?? ?background-color: #F7F7F7; ?? ?font-family: '微軟雅黑'; } form { ?? ?max-width: 640px; ?? ?width: 100%; ?? ?margin: 24px auto; ?? ?font-size: 28px; } label { ?? ?display: block; ?? ?margin: 10px 10px 15px; ?? ?font-size: 24px; } .normal { ?? ?display: block; ?? ?width: 100%; ?? ?height: 40px; ?? ?font-size: 22px; ?? ?margin-top: 10px; ?? ?padding: 6px 10px; ?? ?color: #333; ?? ?border: 1px solid #CCC; ?? ?box-sizing: border-box; } .btn { ?? ?margin-top: 30px; } .btn input { ?? ?color: #FFF; ?? ?background-color: green; ?? ?border: 0 none; ?? ?outline: none; ?? ?cursor: pointer; } input[type="file"] { ?? ?/*opacity: 0;*/ ?? ?width: 120px; ?? ?position: absolute; ?? ?right: 0; ?? ?z-index: 9; } .import { ?? ?height: 40px; ?? ?position: relative; }
index.js
// 引入router模塊 const getRouter = require('router'); // 獲取路由對(duì)象 const router = getRouter(); // 引入模板引擎 const template = require('art-template'); // 學(xué)生學(xué)習(xí)集合 const Student = require('../model/user'); // 引入querystring const querystring = require('querystring'); // 呈遞學(xué)生檔案信息頁(yè)面 router.get('/add',(req, res) => { ? ? let html = template('index.art', {}) ? ? res.end(html); }); // 呈遞學(xué)生檔案信息列表頁(yè)面 router.get('/list', async(req, res) => { ? ? // 查詢學(xué)生信息 ? ? let students = await Student.find(); ? ? console.log(students); ? ? let html = template('list.art', { ? ? ? ? students: students ? ? }) ? ? res.end(html); }); // 實(shí)現(xiàn)學(xué)生信息添加路由功能 router.post('/add', (req, res) => { ? ? // 接受post請(qǐng)求參數(shù) ? ? let formData = ''; ? ? req.on('data', param => { ? ? ? ? formData += param; ? ? }); ? ? req.on('end', async() => { ? ? ? ? await Student.create(querystring.parse(formData)); ? ? ? ? res.writeHead(301, { ? ? ? ? ? ? Location: '/list' ? ? ? ? }) ? ? ? ? res.end(); ? ? }) }) module.exports = router;
index.art
<!DOCTYPE html> <html lang="en"> <head> ?? ?<meta charset="UTF-8"> ?? ?<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> ?? ?<title>學(xué)生檔案</title> ?? ?<link rel="stylesheet" href="./css/main.css" > </head> <body> ?? ?<form action="/add" method="post"> ?? ??? ?<fieldset> ?? ??? ??? ?<legend>學(xué)生檔案</legend> ?? ??? ??? ?<label> ?? ??? ??? ??? ?姓名: <input class="normal" type="text" autofocus placeholder="請(qǐng)輸入姓名" name="name"> ?? ??? ??? ?</label> ?? ??? ??? ?<label> ?? ??? ??? ??? ?年齡: <input class="normal" ?type="text" placeholder="請(qǐng)輸入年齡" name="age"> ?? ??? ??? ?</label> ?? ??? ??? ?<label> ?? ??? ??? ??? ?性別:? ?? ??? ??? ??? ?<input type="radio" value="0" name="sex"> 男 ?? ??? ??? ??? ?<input type="radio" value="1" name="sex"> 女 ?? ??? ??? ?</label> ?? ??? ??? ?<label> ?? ??? ??? ??? ?郵箱地址: <input class="normal" type="text" placeholder="請(qǐng)輸入郵箱地址" name="email"> ?? ??? ??? ?</label> ?? ??? ??? ?<label> ?? ??? ??? ??? ?愛(ài)好:? ?? ??? ??? ??? ?<input type="checkbox" value="敲代碼" name="hobbies"> 敲代碼 ?? ??? ??? ??? ?<input type="checkbox" value="打籃球" name="hobbies"> 打籃球 ?? ??? ??? ??? ?<input type="checkbox" value="睡覺(jué)" name="hobbies"> 睡覺(jué) ?? ??? ??? ?</label> ?? ??? ??? ?<label> ?? ??? ??? ??? ?所屬學(xué)院:? ?? ??? ??? ??? ?<select class="normal" name="collage"> ?? ??? ??? ??? ??? ?<option value="前端與移動(dòng)開(kāi)發(fā)">前端與移動(dòng)開(kāi)發(fā)</option> ?? ??? ??? ??? ??? ?<option value="PHP">PHP</option> ?? ??? ??? ??? ??? ?<option value="JAVA">JAVA</option> ?? ??? ??? ??? ??? ?<option value="Android">Android</option> ?? ??? ??? ??? ??? ?<option value="IOS">IOS</option> ?? ??? ??? ??? ??? ?<option value="UI設(shè)計(jì)">UI設(shè)計(jì)</option> ?? ??? ??? ??? ??? ?<option value="C++">C++</option> ?? ??? ??? ??? ?</select> ?? ??? ??? ?</label> ?? ??? ??? ?<label> ?? ??? ??? ??? ?入學(xué)日期: <input type="date" class="normal" name="enterDate"> ?? ??? ??? ?</label> ?? ??? ??? ?<label class="btn"> ?? ??? ??? ??? ?<input type="submit" value="提交" class="normal"> ?? ??? ??? ?</label> ?? ??? ?</fieldset> ?? ?</form> </body> </html>
list.art
<!DOCTYPE html> <html lang="en"> <head> ?? ?<meta charset="UTF-8"> ?? ?<title>學(xué)員信息</title> ?? ?<link rel="stylesheet" href="./css/list.css" > </head> <body> ?? ?<table> ?? ??? ?<caption>學(xué)員信息</caption> ?? ??? ?<tr> ?? ??? ??? ?<th>姓名</th> ?? ??? ??? ?<th>年齡</th> ?? ??? ??? ?<th>性別</th> ?? ??? ??? ?<th>郵箱地址</th> ?? ??? ??? ?<th>愛(ài)好</th> ?? ??? ??? ?<th>所屬學(xué)院</th> ?? ??? ??? ?<th>入學(xué)時(shí)間</th> ?? ??? ?</tr> ?? ??? ?{{each students}} ?? ??? ??? ?<tr> ?? ??? ??? ??? ?<th>{{$value.name}}</th> ?? ??? ??? ??? ?<th>{{$value.age}}</th> ?? ??? ??? ??? ?<th>{{$value.sex == '0' ? '男' : '女'}}</th> ?? ??? ??? ??? ?<th>{{$value.email}}</th> ?? ??? ??? ??? ?<th> ?? ??? ??? ??? ??? ?{{each $value.hobbies}} ?? ??? ??? ??? ??? ??? ?<span>{{$value}}</span> ?? ??? ??? ??? ??? ?{{/each}} ?? ??? ??? ??? ?</th> ?? ??? ??? ??? ?<th>{{$value.collage}}</th> ?? ??? ??? ??? ?<th>{{dateformat($value.enterDate, 'yyyy-mm-dd')}}</th> ?? ??? ??? ?</tr> ?? ??? ?{{/each}} ?? ??? ? ?? ?</table> </body> </html>
app.js
// 引入http模塊 const http = require('http'); // 引入模板引擎 const template = require('art-template'); // 引入path模塊 const path = require('path'); // 引入靜態(tài)資源訪問(wèn)模塊 const serveStatic = require('serve-static'); // 引入處理日期的第三方模塊 const dateformat = require('dateformat'); // 引入router const router = require('./route/index') // 實(shí)現(xiàn)靜態(tài)資源訪問(wèn)服務(wù) const serve = serveStatic(path.join(__dirname, 'public')); // 配置模板的根目錄 template.defaults.root = path.join(__dirname, 'views'); // 處理日期格式的方法 template.defaults.imports.dateformat = dateformat; // 數(shù)據(jù)庫(kù)連接 require('./model/connect'); // 創(chuàng)建網(wǎng)站服務(wù)器 const app = http.createServer(); // 當(dāng)客戶端訪問(wèn)服務(wù)器的時(shí)候 app.on('request', (req, res) => { ? ? // 啟用路由功能 ? ? router(req, res, () => {}) ? ? // 啟用靜態(tài)資源訪問(wèn)服務(wù)功能 ? ? serve(req, res, () => {}) }); app.listen(3000,() => { ? ? console.log('服務(wù)器創(chuàng)建成功'); });
package.json(需要提前下載第三方模塊)
{ ? "name": "14.students", ? "version": "1.0.0", ? "description": "", ? "main": "app.js", ? "scripts": { ? ? "test": "echo \"Error: no test specified\" && exit 1" ? }, ? "keywords": [], ? "author": "", ? "license": "ISC", ? "dependencies": { ? ? "art-template": "^4.13.2", ? ? "dateformat": "^3.0.3", ? ? "mongoose": "^5.9.18", ? ? "router": "^1.3.5", ? ? "serve-static": "^1.14.1" ? } } // art-template、dateformat、mongoose、router、serve-static
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
從一個(gè)爬蟲(chóng)開(kāi)始ChatGPT的編程秀
這篇文章主要為大家介紹了從一個(gè)爬蟲(chóng)開(kāi)始ChatGPT的編程秀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03node.js的exports、module.exports與ES6的export、export default深入詳解
這篇文章主要給大家介紹了關(guān)于node.js中的exports、module.exports與ES6中的export、export default到時(shí)是什么的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10node前端開(kāi)發(fā)模板引擎Jade的入門(mén)
這篇文章主要介紹了node前端開(kāi)發(fā)模板引擎Jade的入門(mén),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05node 標(biāo)準(zhǔn)輸入流和輸出流代碼實(shí)例
這篇文章主要介紹了node 標(biāo)準(zhǔn)輸入流和輸出流代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Node.js進(jìn)階之核心模塊https入門(mén)
在nodejs中,提供了 https 這個(gè)模塊來(lái)完成 HTTPS 相關(guān)功能。這篇文章主要介紹了Node.js進(jìn)階之核心模塊https入門(mén),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Node中node_modules文件夾及package.json文件的作用說(shuō)明
這篇文章主要介紹了Node中node_modules文件夾及package.json文件的作用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Node.js Addons翻譯(C/C++擴(kuò)展)
這篇文章主要介紹了Node.js Addons翻譯(C/C++擴(kuò)展) 的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06nodejs處理http請(qǐng)求實(shí)例詳解之get和post
最近一段時(shí)間在學(xué)習(xí)前端向服務(wù)器發(fā)送數(shù)據(jù)和請(qǐng)求數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于nodejs處理http請(qǐng)求實(shí)例詳解之get和post的相關(guān)資料,需要的朋友可以參考下2023-01-01