Created
June 3, 2019 03:28
-
-
Save silavsale/1d543179d0a49a4f22858985080dadb0 to your computer and use it in GitHub Desktop.
DB to HTML
This file contains 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 morgan = require('morgan'); | |
const fs = require("fs"); | |
const {Pool, Client} = require('pg'); | |
const bodyParser = require('body-parser'); | |
const contents = fs.readFileSync("user.json"); | |
const jsonContent = JSON.parse(contents); | |
const app = express(); | |
const port = 3000; | |
app.use(morgan('short')); | |
app.use(bodyParser.urlencoded({extended: false})); | |
app.use(express.static('./public')); | |
const client = new Client({ | |
user: 'postgres', | |
host: '127.0.0.1', | |
database: 'nodeTest', | |
port: 5432, | |
}); | |
client.connect(); | |
app.post('/car_create', (req, res) => { | |
const make = req.body.create_make; | |
const model = req.body.create_model; | |
client.query(`INSERT INTO cars (make, model) VALUES ($1, $2) RETURNING car_id , "make", "model"`, [make, model], (error, results) => { | |
if (error) { | |
throw error | |
} | |
res.status(201).send(`Car is : ${results.rows[0].car_id}`); | |
res.end(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment