欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Node.js實現(xiàn)登錄注冊功能

 更新時間:2022年04月26日 15:31:58   作者:Cabu_husky  
這篇文章主要為大家詳細介紹了Node.js實現(xiàn)登錄注冊功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Node.js實現(xiàn)登錄注冊功能的具體代碼,供大家參考,具體內(nèi)容如下

目錄結(jié)構(gòu)

注冊頁面:

reg.html

<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title></title>
? ? <link rel="stylesheet" href="./src/css/reg.css">
</head>
<body>
? ? <div class="reg">
? ? ? ? <h1>用戶注冊</h1>
? ? ? ? <p>
? ? ? ? ? ? <label for="">用戶名:</label>
? ? ? ? ? ? <input type="text" id="username">
? ? ? ? </p>
? ? ? ? <p>
? ? ? ? ? ? <label for="">密&emsp;碼:</label>
? ? ? ? ? ? <input type="text" id="password">
? ? ? ? </p>
? ? ? ? <button>注冊</button>
? ? </div>
</body>
</html>
<script src="./node_modules/jquery/dist/jquery.js"></script>
<script>
?? ?//點擊注冊發(fā)送ajax請求
? ? $('button').eq(0).on('click',()=>{
? ? ? ? $.ajax({
? ? ? ? ? ? url: '/register',
? ? ? ? ? ? type: 'POST',
? ? ? ? ? ? data: {
? ? ? ? ? ? ? ? username : $('#username').val(),
? ? ? ? ? ? ? ? password : $('#password').val()
? ? ? ? ? ? },
? ? ? ? ? ? success: function(res){
? ? ? ? ? ? ? ? switch (res) {
? ? ? ? ? ? ? ? ? ? case '1':
? ? ? ? ? ? ? ? ? ? ? ? alert('成功');
? ? ? ? ? ? ? ? ? ? ? ? window.location.href = "./login.html";
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case '2':
? ? ? ? ? ? ? ? ? ? ? ? alert('失敗');
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case '3':
? ? ? ? ? ? ? ? ? ? ? ? alert('重名');
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case '4':
? ? ? ? ? ? ? ? ? ? ? ? alert('未知錯誤');
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? }
? ? ? ? })
? ? })
</script>

登錄頁面:

login.html

<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title></title>
? ? <link rel="stylesheet" href="./src/css/reg.css">
</head>
<body>
? ? <div class="reg">
? ? ? ? <h1>用戶登錄</h1>
? ? ? ? <p>
? ? ? ? ? ? <label for="">用戶名:</label>
? ? ? ? ? ? <input type="text" id="username">
? ? ? ? </p>
? ? ? ? <p>
? ? ? ? ? ? <label for="">密&emsp;碼:</label>
? ? ? ? ? ? <input type="text" id="password">
? ? ? ? </p>
? ? ? ? <button>登錄</button>
? ? </div>
</body>
</html>

<script src="./node_modules/jquery/dist/jquery.js"></script>
<script>
?? ?//點擊登錄發(fā)送ajax請求
? ? $('button').eq(0).on('click',()=>{
? ? ? ? $.ajax({
? ? ? ? ? ? url: '/login',
? ? ? ? ? ? type: 'GET',
? ? ? ? ? ? data: {
? ? ? ? ? ? ? ? username: $('#username').val(),
? ? ? ? ? ? ? ? password: $('#password').val()
? ? ? ? ? ? },
? ? ? ? ? ? success: function(res){
? ? ? ? ? ? ? ? console.log(res);
? ? ? ? ? ? ? ? switch (res){
? ? ? ? ? ? ? ? ? ? case '1':
? ? ? ? ? ? ? ? ? ? ? ? alert('成功');
? ? ? ? ? ? ? ? ? ? ? ? window.location.href = "./index.html";
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case '2':
? ? ? ? ? ? ? ? ? ? ? ? alert('失敗');
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case '3':
? ? ? ? ? ? ? ? ? ? ? ? alert('密碼錯誤');
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case '4':
? ? ? ? ? ? ? ? ? ? ? ? alert('未知錯誤');
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? })
? ? })
</script>

app.js

const http = require("http");
const fs = require("fs");
const url = require("url");
const querystring = require("querystring");
const post = 3000;
//通過http模塊創(chuàng)建服務器,并監(jiān)聽端口3000
const server = http.createServer();
server.on("request",(req,res)=>{
? ? const dataurl = url.parse(req.url);
? ? //靜態(tài)伺服
? ? //默認進入reg.html頁面?
? ? if((req.url == "/" || req.url == "/reg.html") && req.method == "GET" && req.url != "/favicon.ico"){
? ? ? ? fs.readFile("./reg.html","utf8",(err,data)=>{
? ? ? ? ? ? if(err){
? ? ? ? ? ? ? ? fs.readFile("./404.html","utf8",(err,data)=>{
? ? ? ? ? ? ? ? ? ? res.end(data);
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? ? ? res.setHeader("Content-type","text/html");
? ? ? ? ? ? res.end(data);
? ? ? ? })
? ? //讀取login.html
? ? }else if(req.url == "/login.html" && req.method == "GET"){
? ? ? ? fs.readFile("./login.html","utf8",(err,data)=>{
? ? ? ? ? ? if(err){
? ? ? ? ? ? ? ? fs.readFile("./404.html","uft8",(err,data)=>{
? ? ? ? ? ? ? ? ? ? res.end(data);
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? ? ? res.setHeader("Content-type","text/html");
? ? ? ? ? ? res.end(data);
? ? ? ? })
? ? //讀取index.html
? ? }else if(req.url == "/index.html" && req.method == "GET"){
? ? ? ? fs.readFile("./index.html","utf8",(err,data)=>{
? ? ? ? ? ? if(err){
? ? ? ? ? ? ? ? fs.readFile("./404.html","uft8",(err,data)=>{
? ? ? ? ? ? ? ? ? ? res.end(data);
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? ? ? res.setHeader("Content-type","text/html");
? ? ? ? ? ? res.end(data);
? ? ? ? })
? ? //讀取reg.css
? ? }else if(req.url == "/src/css/reg.css" && req.method == "GET"){
? ? ? ? fs.readFile("src/css/reg.css","utf8",(err,data)=>{
? ? ? ? ? ? if(err){
? ? ? ? ? ? ? ? console.log(err);
? ? ? ? ? ? }
? ? ? ? ? ? res.setHeader("Content-type","text/css");
? ? ? ? ? ? res.end(data);
? ? ? ? })
? ? //讀取jquery
? ? }else if(req.url == "/node_modules/jquery/dist/jquery.js" && req.method == "GET"){
? ? ? ? fs.readFile("./node_modules/jquery/dist/jquery.js","utf8",(err,data)=>{
? ? ? ? ? ? if(err){
? ? ? ? ? ? ? ? console.log(err);
? ? ? ? ? ? }
? ? ? ? ? ? res.end(data);
? ? ? ? })
? ? }
})
server.listen(post);

注冊接口:

/register

else if(req.url == "/register" && req.method == "POST"){
? let str = '';
? ? req.on('data',(chunk)=>{
? ? ? ? str += chunk;
? ? })
? ? req.on('end',()=>{
? ? ? ? let dataObj = querystring.parse(str);
? ? ? ? fs.readFile("./data.json","utf8",(err,data)=>{
? ? ? ? ? ? let obj = JSON.parse(data); ??
? ? ? ? ? ? for(let i = 0; i < obj.length; i++){
? ? ? ? ? ? ? ? if(obj[i].username == dataObj.username){
? ? ? ? ? ? ? ? ? ? return res.end('3');
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? obj.push(dataObj);
? ? ? ? ? ? fs.writeFile('./data.json',JSON.stringify(obj),'utf8',(err,result)=>{
? ? ? ? ? ? ? ? if(err){
? ? ? ? ? ? ? ? ? ? return res.end('2');
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return res.end('1');
? ? ? ? ? ? })
? ? ? ? })
? ? })
}

登錄接口:

/login

else if(dataurl.pathname == "/login" && req.method == "GET"){
? ?console.log(dataurl);
? ? var userInput = querystring.parse(dataurl.query);
? ? fs.readFile("./data.json","utf8",(err,data)=>{
? ? ? ? let obj = JSON.parse(data);
? ? ? ? for(let i = 0; i < obj.length; i++){
? ? ? ? ? ? if(obj[i].username == userInput.username && obj[i].password == userInput.password){
? ? ? ? ? ? ? ? return res.end('1');
? ? ? ? ? ? }else if(obj[i].username == userInput.username && obj[i].password != userInput.password){
? ? ? ? ? ? ? ? return res.end('3');
? ? ? ? ? ? }
? ? ? ? }
? ? })
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • node.js中http模塊和url模塊的簡單介紹

    node.js中http模塊和url模塊的簡單介紹

    這篇文章主要給大家簡單介紹了關(guān)于node.js中的http模塊和url模塊,文中通過示例代碼介紹的非常詳細,對大家學習或者使用node.js具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-10-10
  • nvm使用use命令失效問題解決方法

    nvm使用use命令失效問題解決方法

    這篇文章主要給大家介紹了關(guān)于nvm使用use命令失效問題的解決方法,nvm是一個類似于版本管理工具的軟件,它可以輕松地在同一臺計算機上管理多個不同的node.js版本,需要的朋友可以參考下
    2023-07-07
  • node.js中Socket.IO的進階使用技巧

    node.js中Socket.IO的進階使用技巧

    這篇文章主要介紹了node.js中Socket.IO的進階使用技巧,本文講解了配置、房間、事件、授權(quán)等內(nèi)容,需要的朋友可以參考下
    2014-11-11
  • nodejs中實現(xiàn)修改用戶路由功能

    nodejs中實現(xiàn)修改用戶路由功能

    這篇文章主要介紹了nodejs中實現(xiàn)修改用戶路由功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-05-05
  • nodejs安裝與配置過程+初學實例解讀

    nodejs安裝與配置過程+初學實例解讀

    這篇文章主要介紹了nodejs安裝與配置過程+初學實例解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • nodejs與瀏覽器中全局對象區(qū)別點總結(jié)

    nodejs與瀏覽器中全局對象區(qū)別點總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于nodejs與瀏覽器中全局對象區(qū)別點總結(jié)內(nèi)容,對此有需要的朋友們可以學習下。
    2021-12-12
  • Nodejs下使用gm圓形裁剪并合成圖片的示例

    Nodejs下使用gm圓形裁剪并合成圖片的示例

    本篇文章主要介紹了Nodejs下使用gm圓形裁剪并合成圖片的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Dapr+NestJs編寫Pub及Sub裝飾器實戰(zhàn)示例

    Dapr+NestJs編寫Pub及Sub裝飾器實戰(zhàn)示例

    這篇文章主要為大家介紹了Dapr+NestJs編寫Pub及Sub裝飾器的實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • koa源碼中promise的解讀

    koa源碼中promise的解讀

    這篇文章主要介紹了koa源碼中promise的解讀,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • Nodejs中使用phantom將html轉(zhuǎn)為pdf或圖片格式的方法

    Nodejs中使用phantom將html轉(zhuǎn)為pdf或圖片格式的方法

    這篇文章主要介紹了Nodejs中使用phantom將html轉(zhuǎn)為pdf或圖片格式的方法,需要的朋友可以參考下
    2017-09-09

最新評論