学习目标:此教程将教会大家 如何一步一步实现一个完整的课程学习系统(包括课程管理后台/Node服务器/学习门户三个模块)。
express项目构建 vue-cli项目构建
我们首先给项目取一个名字 “在线课堂” 好啦,英文名 classweb
先在自己喜欢的位置 创建项目目录文件夹 classweb
创建node项目(这个项目我们采取前后端分离的模式,所以需要分别创建node和vue项目,但都放在classweb中)
进入目录,运行 express server 生成服务器端项目(server是我们服务端项目的名字) (这里注意,得提前安装node express-generator,学习过前面node基础的同学这些应该的安装了的,没安装的先学前面的课程)
打开命令行的简单方法:在文件夹中按住 shift 鼠标右键 点击“在此处打开命令行” / "在此处打开powershell窗口"
先安装 cnpm 镜像(它是npm的国内代理,可以使下载速度加快,如果以及安装了的就不用安装了),使用如下代码,安装完成后测试一下 cnpm -v
npm install -g cnpm --registry=https://registry.npm.taobao.org
进入项目,安装依赖,运行测试一下
这样就运行起来了,在浏览器输入http://localhost:3000/ 访问
上面 Node项目就建好了
创建 vue项目
先全局安装vue-cli
npm install --global vue-cli
这里注意,我们最好是另外开一个命令行 来执行,因为开发时前面的Node项目和vue项目要同时运行
然后在创建vue项目,使用 vue init webpack vueclient
注意:ESLint选项要选择no(不然代码一点不规范就报错) ,如果选错了,把vueclient文件夹删了重新创建一遍即可。
进入项目,安装依赖,运行
这时候浏览器中就自动代开网页了
两个项目的安装和测试就完成了
安装SQL server操作软件 SQL2008
百度云链接
链接:网不好一直传不上,等传上去在发布地址,大家也可以自行百度下载
安装方法参考:https://jingyan.baidu.com/article/948f592434b407d80ef5f97d.html
安装完SQL2008之后打开 SQL Server Management Studio(sql图形化工具,当然大神可以直接命令行运行 osql -S 【数据库服务器】 -U 【登陆用户名】 -P 【登陆密码】 出现 1> 表示连接成功,这时候你可以输入sql语句来进行操作了,后面命令创建数据库,表就不赘述了,想学习的自行百度,这里讲解图形化工具)
链接好以后,在数据库右键 新建数据库 输入创建 ForStudy数据库
server/routes下创建文件db.js主要作用连接数据库 封装一下mssql方法
首先我们要安装操作sql server的模块
npm install mssql
db.js封装代码
const mssql = require("mssql");
const conf = require("../config.js");
let restoreDefaults = function () {
conf;
};
const con = new mssql.ConnectionPool(conf);
con.on('error', err => {
if (err) {
throw err;
}
});
con.connect(err => {
if (err) {
console.error(err);
}
});
let querySql = async function (sql, params, callBack) {
try{
let ps = new mssql.PreparedStatement(con);
if (params != "") {
for (var index in params) {
if (typeof params[index] == "number") {
ps.input(index, mssql.Int);
} else if (typeof params[index] == "string") {
ps.input(index, mssql.NVarChar);
}
}
}
ps.prepare(sql, err => {
if (err)
console.log(err);
ps.execute(params, (err, recordset) => {
callBack(err, recordset);
ps.unprepare(err => {
if (err)
console.log(err);
});
});
});
}catch(err){
console.error('SQL error', err);
}
restoreDefaults();
};
var select = async function (tableName, topNumber, whereSql, params, orderSql, callBack) {
try{
var ps = new mssql.PreparedStatement(con);
var sql = "select * from " + tableName + " ";
if (topNumber != "") {
sql = "select top(" + topNumber + ") * from " + tableName + " ";
}
sql += whereSql + " ";
if (params != "") {
console.log(params)
for (var index in params) {
console.log('111111'+params[index])
console.log('222222'+index)
if (typeof params[index] == "number") {
ps.input(index, mssql.Int);
} else if (typeof params[index] == "string") {
ps.input(index, mssql.NVarChar);
}
}
}
sql += orderSql;
console.log(sql);
ps.prepare(sql, err => {
if (err)
console.log(err);
ps.execute(params, (err, recordset) => {
callBack(err, recordset);
ps.unprepare(err => {
if (err)
console.log(err);
});
});
});
}catch(err){
console.error('SQL error', err);
}
restoreDefaults();
};
var selectAll = async function (tableName, callBack) {
try{
var ps = new mssql.PreparedStatement(con);
var sql = "select * from " + tableName + " ";
ps.prepare(sql, err => {
if (err)
console.log(err);
ps.execute("", (err, recordset) => {
callBack(err, recordset);
ps.unprepare(err => {
if (err)
console.log(err);
});
});
});
}catch(err){
console.error('SQL error', err);
}
restoreDefaults();
};
var add = async function (addObj, tableName, callBack) {
try{
var ps = new mssql.PreparedStatement(con);
var sql = "insert into " + tableName + "(";
if (addObj != "") {
for (var index in addObj) {
if (typeof addObj[index] == "number") {
ps.input(index, mssql.Int);
} else if (typeof addObj[index] == "string") {
ps.input(index, mssql.NVarChar);
}
sql += index + ",";
}
sql = sql.substring(0, sql.length - 1) + ") values(";
for (var index in addObj) {
if (typeof addObj[index] == "number") {
sql += addObj[index] + ",";
} else if (typeof addObj[index] == "string") {
sql += "'" + addObj[index] + "'" + ",";
}
}
}
sql = sql.substring(0, sql.length - 1) + ")";
ps.prepare(sql, err => {
if (err)
console.log(err);
ps.execute(addObj, (err, recordset) => {
callBack(err, recordset);
ps.unprepare(err => {
if (err)
console.log(err);
});
});
});
}catch(err){
console.error('SQL error', err);
}
restoreDefaults();
};
var update = async function (updateObj, whereObj, tableName, callBack) {
try{
var ps = new mssql.PreparedStatement(con);
var sql = "update " + tableName + " set ";
if (updateObj != "") {
for (var index in updateObj) {
if (typeof updateObj[index] == "number") {
ps.input(index, mssql.Int);
sql += index + "=" + updateObj[index] + ",";
} else if (typeof updateObj[index] == "string") {
ps.input(index, mssql.NVarChar);
sql += index + "=" + "'" + updateObj[index] + "'" + ",";
}
}
}
sql = sql.substring(0, sql.length - 1) + " where ";
if (whereObj != "") {
for (var index in whereObj) {
if (typeof whereObj[index] == "number") {
ps.input(index, mssql.Int);
sql += index + "=" + whereObj[index] + " and ";
} else if (typeof whereObj[index] == "string") {
ps.input(index, mssql.NVarChar);
sql += index + "=" + "'" + whereObj[index] + "'" + " and ";
}
}
}
sql = sql.substring(0, sql.length - 5);
ps.prepare(sql, err => {
if (err)
console.log(err);
ps.execute(updateObj, (err, recordset) => {
callBack(err, recordset);
ps.unprepare(err => {
if (err)
console.log(err);
});
});
});
}catch(err){
console.error('SQL error', err);
}
restoreDefaults();
};
var del = async function (whereSql, params, tableName, callBack) {
try{
var ps = new mssql.PreparedStatement(con);
var sql = "delete from " + tableName + " ";
if (params != "") {
for (var index in params) {
if (typeof params[index] == "number") {
ps.input(index, mssql.Int);
} else if (typeof params[index] == "string") {
ps.input(index, mssql.NVarChar);
}
}
}
sql += whereSql;
ps.prepare(sql, err => {
if (err)
console.log(err);
ps.execute(params, (err, recordset) => {
callBack(err, recordset);
ps.unprepare(err => {
if (err)
console.log(err);
});
});
});
}catch(err){
console.error('SQL error', err);
}
restoreDefaults();
};
exports.config = conf;
exports.del = del;
exports.select = select;
exports.update = update;
exports.querySql = querySql;
exports.selectAll = selectAll;
exports.restoreDefaults = restoreDefaults;
exports.add = add;
config.js配置代码
let app = {
user: 'sa', //用户名
password: '123456789', //密码
server: 'localhost', //服务器
database: 'ForStudy', //数据库
port: 1433, //端口默认不用改
options: {
encrypt: true // Use this if you're on Windows Azure
},
pool: {
min: 0,
max: 10,
idleTimeoutMillis: 3000
}
};
module.exports = app;
安装 express-session
cnpm install express-session --save
重启服务
刷新vue的登录页面,点击登录
你会发现,控制台打印出了返回的登录成功信息,这样我们的登录功能就编写完成了 (常见出错原因在后面附录)
附录:常见报错
1. 数据库连接失败 :
数据库名没写对 检查config.js中的下图名字是否和数据库名称一样。