장준영

BE-feat : DB에 data 추가

- mysql2 createPool을 이용한 db 연동
- db에 data를 넣기 위한 data_insert 파일 작성
module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true,
node: true,
},
......
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
let createError = require('http-errors');
let express = require('express');
let path = require('path');
let cookieParser = require('cookie-parser');
let logger = require('morgan');
var indexRouter = require('./routes/index');
let indexRouter = require('./routes/index');
var app = express();
let app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
......
const db = require('../model/db');
const profanity1 = require('./profanity1.json');
const profanity2 = require('./profanity2.json');
const insertDB = async () => {
let sumOfProfanity = [...new Set([...profanity1.profanity,...profanity2.profanity])];
sumOfProfanity = sumOfProfanity.map((element) => [element]);
try {
const insertQuery = "insert into profanity (content) values ?;";
const result = await db.query(insertQuery, [sumOfProfanity]);
console.log('yes');
} catch (err) {
console.log(err);
}
}
module.exports = insertDB;
\ No newline at end of file
const mysql = require('mysql2/promise');
const dotenv = require('dotenv');
const path = require('path');
dotenv.config({path:path.join(__dirname, "../.env")});
const db = mysql
.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
port: process.env.DB_PORT,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
connectionLimit: 10,
});
module.exports = db;
......@@ -9,10 +9,12 @@
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"dotenv": "^8.2.0",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"morgan": "~1.9.1"
"morgan": "~1.9.1",
"mysql2": "^2.2.5"
},
"devDependencies": {
"eslint": "^7.15.0"
......
var express = require('express');
const insertDB = require('../data_json/data_insert');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.json('index3');
res.send('hello');
});
module.exports = router;
......