Created
December 8, 2019 19:23
-
-
Save ylynfatt/4da3de6804cad61342d62d22248af2a4 to your computer and use it in GitHub Desktop.
NodeJS MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const mysql = require('mysql'); | |
const bodyParser = require('body-parser'); | |
const con = mysql.createConnection({ | |
host: "localhost", | |
user: "cbmis", | |
password:"cbmispass", | |
database: "laptopshop" | |
}); | |
con.connect(function(err) { | |
if (err) throw err; | |
console.log("Connected!"); | |
}); | |
let app = express(); | |
app.use( bodyParser.json() ); // to support JSON-encoded bodies | |
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies | |
extended: true | |
})); | |
app.get('/', function (res) { | |
res.send('Is this working?'); | |
}) | |
app.post('/register-user', async function (req, res) { | |
let username = req.body.uname, | |
fname = req.body.fname, | |
lname = req.body.lname, | |
email = req.body.email, | |
passwd = req.body.psswd, | |
passw = req.body.psswd2; | |
let name; | |
console.log(" Testing") | |
let usql = 'SELECT username FROM user WHERE username = "'+ username +'"'; | |
let name = await con.query(usql, function (error, results, fields) { | |
if (error) throw error; | |
return results; | |
}); | |
console.log(name) | |
try { | |
//withTransaction( con, async () => { | |
// let someRows = await con.query(usql); | |
// const otherRows = await db.query( 'SELECT * FROM other_table' ); | |
// do something with someRows and otherRows | |
// console.log(someRows[0]) | |
// } ); | |
} catch ( err ) { | |
// handle error | |
console.log(err) | |
alert(err); | |
} | |
/* con.query(usql, function (err, result) { | |
if (err) throw err; | |
console.log(result[0]["username"]); | |
name = result[0]["username"]; | |
//return name; | |
//res.send(result[0]["username"]); | |
//alert(name); | |
});*/ | |
let sql = 'INSERT INTO user (username,fname,lname,email,passwd) VALUES ("'+ username +'","'+ fname +'","'+ lname +'","'+email+'","'+passwd+'")'; | |
/* con.query(sql, function (err, result) { | |
if (err) throw err; | |
res.send(result); | |
});*/ | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment