Showing
12 changed files
with
132 additions
and
8 deletions
api/card/index.js
0 → 100644
api/deck/index.js
0 → 100644
api/index.js
0 → 100644
api/user/index.js
0 → 100644
api/user/signUp.js
0 → 100644
1 | +const express=require('express') | ||
2 | +const session=require('express-session') | ||
3 | +const findById=require('../../database/user/findById') | ||
4 | +const mysql=require('mysql') | ||
5 | +const bcrypt=require('bcrypt-nodejs') | ||
6 | +const dbconfig=require('../../dbconfig') | ||
7 | +const connection=mysql.createConnection(dbconfig) | ||
8 | +connection.connect() | ||
9 | + | ||
10 | +exports.SignUp=(req,res)=>{ | ||
11 | + const userId=req.body.userId | ||
12 | + const password=req.body.password | ||
13 | + const DataCheck=()=>{ | ||
14 | + return new Promise((resolve,reject)=>{ | ||
15 | + if(!userId || !password){ | ||
16 | + return reject({ | ||
17 | + code: 'request_body_error', | ||
18 | + message: 'request body is not defined' | ||
19 | + }) | ||
20 | + } | ||
21 | + else resolve() | ||
22 | + }) | ||
23 | + } | ||
24 | + const SignUp=()=>{ | ||
25 | + const user=findById(userId) | ||
26 | + if(!user){ | ||
27 | + const hash=bcrypt.hashSync(password,bcrypt.genSaltSync(10),null) | ||
28 | + connection.query(`insert into user (userId,password) values (${userId},${hash});`,(err,results,fields)=>{ | ||
29 | + if(err){ | ||
30 | + throw err | ||
31 | + } | ||
32 | + else{ | ||
33 | + console.log(results) | ||
34 | + } | ||
35 | + }) | ||
36 | + } | ||
37 | + else{ | ||
38 | + return reject({ | ||
39 | + code:'User_already_exists', | ||
40 | + message:'User already exists' | ||
41 | + }) | ||
42 | + } | ||
43 | + } | ||
44 | + | ||
45 | + DataCheck() | ||
46 | + .then(SignUp) | ||
47 | + .then(()=>{ | ||
48 | + return res.status(200).json({userId:userId}) | ||
49 | + }) | ||
50 | + .catch((err)=>{ | ||
51 | + console.log(err) | ||
52 | + res.status(500).json(err.message||err) | ||
53 | + }) | ||
54 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -9,6 +9,8 @@ const morgan=require('morgan') | ... | @@ -9,6 +9,8 @@ const morgan=require('morgan') |
9 | const cheerio=require('cheerio') | 9 | const cheerio=require('cheerio') |
10 | const mysql=require('mysql') | 10 | const mysql=require('mysql') |
11 | require('dotenv').config() | 11 | require('dotenv').config() |
12 | +app.use('/api',require('./api')) | ||
13 | +const dbconfig=require('dbconfig') | ||
12 | const app=express() | 14 | const app=express() |
13 | 15 | ||
14 | app.use(morgan('[:date[iso]] :method :status :url :response-time(ms) :user-agent')) | 16 | app.use(morgan('[:date[iso]] :method :status :url :response-time(ms) :user-agent')) |
... | @@ -23,13 +25,7 @@ app.use(function (req, res, next) { | ... | @@ -23,13 +25,7 @@ app.use(function (req, res, next) { |
23 | next() | 25 | next() |
24 | }) | 26 | }) |
25 | 27 | ||
26 | -const connection=mysql.createConnection({ | 28 | +const connection=mysql.createConnection(dbconfig) |
27 | - host:process.env.DB_HOST, | ||
28 | - user:process.env.DB_USER, | ||
29 | - password:process.env.DB_PASSWORD, | ||
30 | - database:'user', | ||
31 | - port:'' | ||
32 | -}) | ||
33 | 29 | ||
34 | let allCards=[] | 30 | let allCards=[] |
35 | fs.readFile('cardskoKR.json',(err,data)=>{ | 31 | fs.readFile('cardskoKR.json',(err,data)=>{ |
... | @@ -50,8 +46,10 @@ app.get('/main',(req,res)=>{ | ... | @@ -50,8 +46,10 @@ app.get('/main',(req,res)=>{ |
50 | if(!req.session.sid) | 46 | if(!req.session.sid) |
51 | res.redirect('/login') | 47 | res.redirect('/login') |
52 | else { | 48 | else { |
53 | - fs.readFile('./views/main') | 49 | + fs.readFile('./views/main',(err,data)=>{ |
54 | res.writeHead(200, {'Content-Type': 'text/html'}) | 50 | res.writeHead(200, {'Content-Type': 'text/html'}) |
51 | + res.end(data) | ||
52 | + }) | ||
55 | } | 53 | } |
56 | }) | 54 | }) |
57 | 55 | ... | ... |
database/card/index.js
0 → 100644
database/deck/index.js
0 → 100644
database/index.js
0 → 100644
database/user/findById.js
0 → 100644
1 | +const mysql=require('mysql') | ||
2 | +const dbconfig=require('../../dbconfig') | ||
3 | + | ||
4 | +exports.findById=(userId)=>{ | ||
5 | + const connection=mysql.createConnection(dbconfig) | ||
6 | + connection.connect() | ||
7 | + | ||
8 | + connection.query(`select * from user where userId=${userId}`,(err,result,fields)=>{ | ||
9 | + if(err){ | ||
10 | + throw err | ||
11 | + } | ||
12 | + else{ | ||
13 | + return result | ||
14 | + } | ||
15 | + }) | ||
16 | +} | ||
17 | + | ||
18 | + |
database/user/index.js
0 → 100644
dbconfig.js
0 → 100644
1 | +require('dotenv').config() | ||
2 | +const mysql=require('mysql') | ||
3 | + | ||
4 | +module.exports={ | ||
5 | + host:process.env.DB_HOST, | ||
6 | + user:process.env.DB_USER, | ||
7 | + password:process.env.DB_PASSWORD, | ||
8 | + database:'WhoAreYou', | ||
9 | + port:process.env.DB_PORT | ||
10 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment