前端處理server表示很蛋疼,初學Node,雖然感覺異常強大,但是學起來還是有些吃力的,Node是工具,它不是萬能的,搭建一個系統還是需要借助其他一些工具,對於我這個沒怎么接觸server的前端來說,挑戰是有的。昨天參考一些資料嘗試用Node+express+mongodb搭建一個簡易的登陸系統,在此記之。
express是一個靈活的nodejs web應用框架, 提供一系列強大特性幫助你創建各種Web應用。
Mongodb是數據庫。
1、安裝express,express安裝比較簡單,直接用npm install -g express-generator,一定要用這個,如果用npm install -g express就會出現express not found的問題。
2、安裝mongodb, 在官網下下在程序,然后一步步安裝,我是在F盤的/software3下新建一個mongodb的文件夾,安裝在該文件夾內。
3、啟動mongodb,cmd中命令如下:
如果看到下面的內容,恭喜啟動成功。最好建一個cmd文件,每次啟動運行該文件即可。

上面遇到一個小的報錯:
WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-12-23T09:21:00.012+0800 I CONTROL [initandlisten]
2018-12-23T09:21:00.012+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-12-23T09:21:00.015+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-12-23T09:21:00.017+0800 I CONTROL [initandlisten]
2018-12-23T09:21:00.020+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-12-23T09:21:00.020+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-12-23T09:21:00.022+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2018-12-23T09:21:00.023+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-12-23T09:21:00.026+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-12-23T09:21:00.027+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
原因分析:新版本的MongDB增加了安全性設計,推薦用戶創建使用數據庫時進行驗證。如果用戶想建立簡單連接,則會提示警示信息。
解決方案:
創建管理員並設置密碼
先執行mongo 進入MongoDB Shell,

補充:MongoDB Shell是MongoDB自帶的交互式Javascript shell,用來對MongoDB進行操作和管理的交互式環境。
由於它是一個JavaScript shell,您可以運行一些簡單的算術運算,詳細這里暫時不講了,下一節講MongoDB Shell:

然后執行 use admin 然后創建用戶:
>db.createUser( { user: "admin", //用戶名 pwd: "123456", //密碼 roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] //設置權限 } )

重啟數據庫服務器
mongod --auth --port 27017 --dbpath F:\software3\mogodb\bin
“–auth”命令即表示訪問數據庫需要認證。此處可不指定端口,則默認為27017。
啟動后即可看到不再提示報警信息。

mongodb.cmd文件:
:: 定位到D盤 d: :: 切換到mongodb的數據庫目錄 cd Mongodb :: 刪除數據庫鎖定記錄文件 if exist mongod.lock del mongod.lock missing :: 配置mongodb的文檔存儲目錄 mongod --dbpath "D:\Mongodb\data"
5、在Mongodb目錄下運行mongo,默認鏈接到本地端口:27017

到這里整個環境配置完畢並且已經啟動。
下面看具體的Node。
安裝完express后在某個目錄下運行express Login -e就會自動創建一個項目,然后npm install會下載依賴包。
1、新建一個models文件夾,在該文件夾下新建user.js:
var mongoose = require("mongoose"); // 頂會議用戶組件
var Schema = mongoose.Schema; // 創建模型
var userScheMa = new Schema({
userid: String,
password: String
}); // 定義了一個新的模型,但是此模式還未和users集合有關聯
exports.user = mongoose.model('users', userScheMa); // 與users集合關聯
2、在views下面建index.ejs, errors.ejs, login.ejs, logout.ejs, homepage.ejs。 (index是自帶的,不用建)
index.ejs:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Hello World</h1>
<p>Welcome to <%= title %></p>
<p><a href="login">登陸</a></p>
</body>
</html>
login.ejs:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Hello World</h1>
<p>Welcome to <%= title %></p>
<form action="homepage" method="post">
<p>
<span>userId:</span>
<br>
<input id="userid" name="userid" type="text">
</p>
<p>
<span>password:</span>
<br>
<input id="password" name="password" type="password">
</p>
<p><input type="submit" value="submit"></p>
</form>
</body>
</html>
loginout.ejs:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Hello World</h1>
<p>Welcome to <%= title %></p>
<p>正在登出...</p>
<script type="text/javascript">
setTimeout(function(){
window.location.href = "/";
}, 500);
</script>
</body>
</html>
homepage.ejs:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Hello World</h1>
<p>Welcome to <%= title %></p>
<p><a href="logout">登出</a></p>
</body>
</html>
3、在routes目下的index.js配置路由:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var user = require('../models/user').user;
mongoose.connect('mongodb://localhost/admin');
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'index' });
});
/*login*/
router.get('/login', function(req, res) {
res.render('login', { title: 'login' });
});
/*logout*/
router.get('/logout', function(req, res) {
res.render('logout', { title: 'logout' });
});
/*hompage*/
router.post('/homepage', function(req, res) {
var query_doc = {userid: req.body.userid, password: req.body.password};
(function(){
user.count(query_doc, function(err, doc){
if(doc == 1){
console.log(query_doc.userid + ": login success in " + new Date());
res.render('homepage', { title: 'homepage' });
}else{
console.log(query_doc.userid + ": login failed in " + new Date());
res.redirect('/');
}
});
})(query_doc);
});
module.exports = router;
大功告成。
確保啟動mongodb后在Login項目下運行npm start。然后在瀏覽器輸入https://localhost:3000就能看到界面了:

漏了一點,需要在mongodb創建用戶名和密碼,不然沒法登陸進去。步驟如下:


