backend-init

master
P-Jhao 3 days ago
parent 4bdc5aa838
commit 236ce04786

@ -0,0 +1,4 @@
DB_NAME = "uml"
DB_USER = "root"
DB_PASSWORD = "123456"
DB_HOST = "localhost"

24
.gitignore vendored

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

@ -0,0 +1,45 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
//优先读取环境变量
require("dotenv").config()
//引入数据库连接
require("./dao/db")
//引入路由
const userRouter = require('./routes/user');
var app = express();
//使用各种中间件
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//使用路由中间件
app.use('/', userRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// 错误捕获
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
});
module.exports = app;

@ -0,0 +1,91 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('backend:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string' ?
'Pipe ' + port :
'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string' ?
'pipe ' + addr :
'port ' + addr.port;
debug('Listening on ' + bind);
console.log("监听3000端口")
}

@ -0,0 +1,24 @@
const sequelize = require("./dbConnect")
const userModel = require("./model/userModel")
const md5 = require("md5")
//将数据模型和表进行同步
async function test() {
await sequelize.sync({
alter: true
})
//将需要数据的表初始化
const userCount = await userModel.count()
if (!userCount) {
//需要初始化
await userModel.create({
loginId: "test",
loginPwd: md5("123")
})
console.log("用户数据初始化完毕")
}
console.log("数据库数据准备完成")
}
test()

@ -0,0 +1,12 @@
const {
Sequelize
} = require("sequelize")
const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, {
host: process.env.DB_HOST,
dialect: "mysql",
logging: false
});
module.exports = sequelize

@ -0,0 +1,19 @@
const sequelize = require("../dbConnect")
const {
DataTypes
} = require("sequelize")
module.exports = sequelize.define("user", {
loginId: {
type: DataTypes.STRING,
allowNull: false
},
loginPwd: {
type: DataTypes.STRING,
allowNull: false
}
}, {
freezeTableName: true,
createdAt: false,
updatedAt: false,
})

1440
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,21 @@
{
"name": "backend",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "nodemon -x npm run server",
"server": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"dotenv": "^16.4.5",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"md5": "^2.3.0",
"morgan": "~1.9.1",
"mysql2": "^3.11.4",
"sequelize": "^6.37.5"
}
}

@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}

@ -0,0 +1,10 @@
var express = require('express');
var router = express.Router();
/* GET home page. */
router.post('/login', async function (req, res, next) {
console.log("loging...")
});
module.exports = router;
Loading…
Cancel
Save