0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

expressとmysqlを使ったDBにbcryptでハッシュ化したデータを挿入する

Posted at

全体のコードです。

sqlでinsertしているpasswordをハッシュ化してから、DBに保存している。

//モジュールをインストール
const { request } = require('express')
const express = require('express')
const app = express()
const port = 5000
const bcrypt = require('bcrypt')
//saltRoundsを使って何回ハッシュ化を行うのか設定
const saltRounds = 10

const mysql = require('mysql');

//DBに接続
const con = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'express_db'
})

//データを挿入するSQL文
const insert = 'INSERT INTO USERS (name, email, password) value (?,?,?)'

//
bcrypt.hash("password", saltRounds, (err, hash) => {
//DB操作
  con.query(insert,["johndoe","john@example.com",hash],function(err, result, fields){
    if (err) throw err;
//結果をログで表示する
    console.log(result);
  })
})


app.get('/',(request, response) => response.send('hello'))

app.listen(port, () => console.log(`example app listening on port ${port}!`))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?