feat:数据库配置等
This commit is contained in:
commit
a73e44d49b
30
.gitignore
vendored
Normal file
30
.gitignore
vendored
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
pnpm-debug.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
.DS_Store
|
||||||
|
dist
|
||||||
|
dist-ssr
|
||||||
|
coverage
|
||||||
|
*.local
|
||||||
|
|
||||||
|
/cypress/videos/
|
||||||
|
/cypress/screenshots/
|
||||||
|
|
||||||
|
# Editor directories and files
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/extensions.json
|
||||||
|
.idea
|
||||||
|
*.suo
|
||||||
|
*.ntvs*
|
||||||
|
*.njsproj
|
||||||
|
*.sln
|
||||||
|
*.sw?
|
||||||
|
|
||||||
|
*.tsbuildinfo
|
11
PG/index.js
Normal file
11
PG/index.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const { Pool } = require('pg')
|
||||||
|
|
||||||
|
const pool = new Pool({
|
||||||
|
user: 'postgres',
|
||||||
|
host: 'yhm.ink',
|
||||||
|
database: 'postgres',
|
||||||
|
password: '123456',
|
||||||
|
port: 54321,
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = pool
|
77
PG/knex.js
Normal file
77
PG/knex.js
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
const knex = require('knex')({
|
||||||
|
client: 'pg',
|
||||||
|
connection: {
|
||||||
|
host: 'yhm.ink',
|
||||||
|
user: 'postgres',
|
||||||
|
password: '123456',
|
||||||
|
database: 'sandbox',
|
||||||
|
port: 54321,
|
||||||
|
},
|
||||||
|
// connection:`postgres://postgres:123456@yhm.ink:54321/sandbox`
|
||||||
|
})
|
||||||
|
|
||||||
|
async function createTableIfNotExist({ tableName, columns }) {
|
||||||
|
const exists = await knex.schema.hasTable(tableName)
|
||||||
|
if (!exists) {
|
||||||
|
await knex.schema.createTable(tableName, (table) => {
|
||||||
|
table.increments('id').primary()
|
||||||
|
table.timestamp('created_at').defaultTo(knex.fn.now())
|
||||||
|
table.timestamp('updated_at').defaultTo(knex.fn.now())
|
||||||
|
columns.forEach((column) => {
|
||||||
|
const { name, type, references } = column
|
||||||
|
const tableExist = table[type](name)
|
||||||
|
references && tableExist.references(references).onDelete('CASCADE')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
console.log(`table ${tableName} created`)
|
||||||
|
} else {
|
||||||
|
console.log(`table ${tableName} exists`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createTables(tables) {
|
||||||
|
for (const table of tables) {
|
||||||
|
await createTableIfNotExist(table)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const tables = [
|
||||||
|
{
|
||||||
|
tableName: 'types',
|
||||||
|
columns: [
|
||||||
|
{ name: 'name', type: 'string' },
|
||||||
|
{ name: 'parent_id', type: 'integer', references: 'types.id' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tableName: 'codes',
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
name: 'type_id',
|
||||||
|
type: 'integer',
|
||||||
|
references: 'types.id',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'title',
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'description',
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'code',
|
||||||
|
type: 'json',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'preview',
|
||||||
|
type: 'text',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
createTables(tables)
|
||||||
|
module.exports = {
|
||||||
|
knex,
|
||||||
|
}
|
9
PG/model/codes.js
Normal file
9
PG/model/codes.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
const Model = require('../objection')
|
||||||
|
|
||||||
|
class Codes extends Model {
|
||||||
|
static get tableName() {
|
||||||
|
return 'codes'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Codes
|
9
PG/model/types.js
Normal file
9
PG/model/types.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
const Model = require('../objection')
|
||||||
|
|
||||||
|
class Types extends Model {
|
||||||
|
static get tableName() {
|
||||||
|
return 'types'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Types
|
7
PG/objection.js
Normal file
7
PG/objection.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
const { Model } = require('objection')
|
||||||
|
|
||||||
|
const { knex } = require('./knex')
|
||||||
|
|
||||||
|
Model.knex(knex)
|
||||||
|
|
||||||
|
module.exports = Model
|
43
app.js
Normal file
43
app.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
var createError = require('http-errors')
|
||||||
|
var express = require('express')
|
||||||
|
var path = require('path')
|
||||||
|
var cookieParser = require('cookie-parser')
|
||||||
|
var logger = require('morgan')
|
||||||
|
|
||||||
|
var indexRouter = require('./routes/index')
|
||||||
|
var usersRouter = require('./routes/users')
|
||||||
|
var codesRouter = require('./routes/Codes')
|
||||||
|
|
||||||
|
var app = express()
|
||||||
|
|
||||||
|
// view engine setup
|
||||||
|
app.set('views', path.join(__dirname, 'views'))
|
||||||
|
app.set('view engine', 'jade')
|
||||||
|
|
||||||
|
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('/', indexRouter)
|
||||||
|
app.use('/users', usersRouter)
|
||||||
|
app.use('/codes', codesRouter)
|
||||||
|
|
||||||
|
// catch 404 and forward to error handler
|
||||||
|
app.use(function (req, res, next) {
|
||||||
|
next(createError(404))
|
||||||
|
})
|
||||||
|
|
||||||
|
// error handler
|
||||||
|
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)
|
||||||
|
res.render('error')
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = app
|
90
bin/www
Executable file
90
bin/www
Executable file
@ -0,0 +1,90 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var app = require('../app');
|
||||||
|
var debug = require('debug')('expresssandbox: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);
|
||||||
|
}
|
1874
package-lock.json
generated
Normal file
1874
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
package.json
Normal file
20
package.json
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "expresssandbox",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"start": "nodemon --watch routes ./bin/www"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"cookie-parser": "~1.4.4",
|
||||||
|
"debug": "~2.6.9",
|
||||||
|
"express": "~4.16.1",
|
||||||
|
"http-errors": "~1.6.3",
|
||||||
|
"jade": "~1.11.0",
|
||||||
|
"knex": "^3.1.0",
|
||||||
|
"morgan": "~1.9.1",
|
||||||
|
"nodemon": "^3.1.7",
|
||||||
|
"objection": "^3.1.5",
|
||||||
|
"pg": "^8.13.1"
|
||||||
|
}
|
||||||
|
}
|
8
public/stylesheets/style.css
Normal file
8
public/stylesheets/style.css
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
body {
|
||||||
|
padding: 50px;
|
||||||
|
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: #00B7FF;
|
||||||
|
}
|
15
routes/Codes/index.js
Normal file
15
routes/Codes/index.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
const { getCodeListByTypeId } = require('./service')
|
||||||
|
const { sendResponse } = require('../../utils')
|
||||||
|
|
||||||
|
const express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
|
|
||||||
|
router.get('/list/:typeId', async (req, res) => {
|
||||||
|
const { typeId } = req.params
|
||||||
|
|
||||||
|
const gCodeList = await getCodeListByTypeId(typeId)
|
||||||
|
|
||||||
|
sendResponse({ res, status: 200, message: '', data: gCodeList })
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = router
|
11
routes/Codes/service/index.js
Normal file
11
routes/Codes/service/index.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const Codes = require('../../../PG/model/codes')
|
||||||
|
|
||||||
|
async function getCodeListByTypeId(type_id) {
|
||||||
|
return type_id
|
||||||
|
? await Codes.query().where('type_id', type_id)
|
||||||
|
: await Codes.query()
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getCodeListByTypeId,
|
||||||
|
}
|
9
routes/index.js
Normal file
9
routes/index.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router();
|
||||||
|
|
||||||
|
/* GET home page. */
|
||||||
|
router.get('/', function(req, res, next) {
|
||||||
|
res.render('index', { title: 'Express' });
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
9
routes/users.js
Normal file
9
routes/users.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var router = express.Router();
|
||||||
|
|
||||||
|
/* GET users listing. */
|
||||||
|
router.get('/', function(req, res, next) {
|
||||||
|
res.send('respond with a resource');
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
13
utils/index.js
Normal file
13
utils/index.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
function sendResponse({ res, status, message, data }) {
|
||||||
|
const response = {
|
||||||
|
code: status,
|
||||||
|
status: status === 200 ? 'success' : 'error',
|
||||||
|
massage: message,
|
||||||
|
data: data,
|
||||||
|
}
|
||||||
|
res.status(status).send(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
sendResponse,
|
||||||
|
}
|
6
views/error.jade
Normal file
6
views/error.jade
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
extends layout
|
||||||
|
|
||||||
|
block content
|
||||||
|
h1= message
|
||||||
|
h2= error.status
|
||||||
|
pre #{error.stack}
|
5
views/index.jade
Normal file
5
views/index.jade
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
extends layout
|
||||||
|
|
||||||
|
block content
|
||||||
|
h1= title
|
||||||
|
p Welcome to #{title}
|
7
views/layout.jade
Normal file
7
views/layout.jade
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
doctype html
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title= title
|
||||||
|
link(rel='stylesheet', href='/stylesheets/style.css')
|
||||||
|
body
|
||||||
|
block content
|
Loading…
Reference in New Issue
Block a user