adduser.js 1.42 KB
var express = require('express');
var fs = require('fs');
var router = express.Router();
var mysql = require('mysql');

var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'soap',
    password : '111111',
    port     : 3306,
    database : 'ytmt'
});

router.get('/', function(req, res) {
    var username = [];
    connection.query("SELECT username FROM user WHERE id = " + req.user.user_id,[username], function(err, result) {
        if(!err) {
            if(result[0].username == null) {
                fs.readFile('./views/adduser.ejs', function(err, data) {
                    if(err) {console.log(err);}
                    else {
                        res.writeHead(200, {'Content-Type' : 'text/html'});
                        res.end(data);
                    } 
                });
            }else {
                res.redirect('/mytoons');
            }
        }
        else {
            console.log('DB : ERROR OCCURS');
        }
    });
    
});

router.post('/addtodb', (req, res) => {
    var user_name = req.body.username;
    var sqlquery = "UPDATE user SET username = '" + user_name +"' WHERE id = " + req.user.user_id;
    connection.query(sqlquery,user_name,function(err) {
        if(err) {
            res.send("DATA BASE : ERROR OCCURS");
        }
        else {
            // res.send("SUCCESS!");
            res.redirect('/mytoons');
        }
    })
})

module.exports = router;