用原生nodejs寫一個簡單博客的后端,僅供學(xué)習(xí)用,實際開發(fā)中肯定用框架好,用原生寫一遍能更好幫助你理解nodejs原理,讓你使用nodejs更加得心應(yīng)手。
GitHub倉庫地址
https://github.com/rongliangtang/nodejs-for-blog-backend
項目實現(xiàn)
- 數(shù)據(jù)庫開發(fā)
- 接口開發(fā)
- 登錄:cookie和session的配合來存儲登錄狀態(tài)
- 日志記錄以及分析
- 安全:防止sql注入,xss攻擊,密碼加密
項目數(shù)據(jù)庫設(shè)計

數(shù)據(jù)庫設(shè)計

數(shù)據(jù)庫設(shè)計
項目接口設(shè)計

接口設(shè)計
項目結(jié)構(gòu)解析

項目結(jié)構(gòu)
啟動前需要更改數(shù)據(jù)庫配置和安裝redis
數(shù)據(jù)庫配置在src/config/db目錄下,更改為你電腦上的數(shù)據(jù)庫配置</br>
mysql中運行以下代碼創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù):
create database blog;
use blog;
create table user(
id INT(11) auto_increment primary key,
username VARCHAR(30) NOT null unique,
password VARCHAR(30) NOT null
);
create table classification(
id INT(11) auto_increment primary key,
name VARCHAR(30) NOT null unique,
number INT(11) default 0
);
create table article(
id INT(11) auto_increment primary key,
title VARCHAR(30) NOT null,
content longtext NOT null,
createtime bigint not null ,
classification INT(11)
);
insert into user (username,`password`) values ('admin','123456');
insert into user (username,`password`) values ('管理員','123456');
insert into classification (name,number) values ('分類1',2);
insert into classification (name) values ('分類2');
insert into article (title,content,createtime,classification) values('標(biāo)題1','內(nèi)容1',1111111111,1);
insert into article (title,content,createtime,classification) values('標(biāo)題2','內(nèi)容2',1112222222,1);
安裝和啟動redis參考</br>https://www.runoob.com/redis/redis-install.html</br>(注意啟動后cmd窗口不可關(guān)閉)
啟動前先安裝項目所需要的依賴
npm install
編譯運行
npm run dev