Skip to content

Instantly share code, notes, and snippets.

@gen-yamada
Created December 11, 2020 03:18
Show Gist options
  • Save gen-yamada/12f09081f73f6a3cd5b870ea09a7b017 to your computer and use it in GitHub Desktop.
Save gen-yamada/12f09081f73f6a3cd5b870ea09a7b017 to your computer and use it in GitHub Desktop.
express connect-mysql nuxt.js
const express = require("express")
const app = express()
module.exports = {
path: "/server",
handler: app,
}
app.get("/category", (req, res) => {
const mysql = require("mysql")
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "database-name",
password: "type-your-password",
})
connection.connect()
let sql = "select * from table-name"
connection.query(sql, function (error, row, fields) {
if (error) {
console.log(error)
}
let dat = []
for (let i = 0; i < row.length; i++) {
dat.push({
id: row[i].column-name,
name: row[i].column-name,
})
}
ret = JSON.stringify(dat)
res.send(ret)
})
connection.end()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment