Skip to content

Instantly share code, notes, and snippets.

@silavsale
Created June 3, 2019 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silavsale/1d543179d0a49a4f22858985080dadb0 to your computer and use it in GitHub Desktop.
Save silavsale/1d543179d0a49a4f22858985080dadb0 to your computer and use it in GitHub Desktop.
DB to HTML
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