Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mysql = require('mysql');
- const express = require('express');
- const app = express();
- var con = mysql.createConnection({
- host: "52.208.53.100",
- user: "ssostag",
- password: "sskDwm8X",
- database: "test"
- });
- con.connect(function(err) {
- if(err) throw err;
- console.log("Connected!");
- app.use(express.json());
- app.use(express.urlencoded({
- extended: true
- }));
- app.get('/', function(req, res) {
- res.send({
- "api-version": "1.0.0"
- });
- });
- app.get('/customers', function (req, res)
- {
- con.query("SELECT * FROM customer", function (err, result, fields) {
- // if (err) throw err;
- res.send(result)
- });
- });
- app.get('/customers/:id', function(req, res) {
- con.query("SELECT * FROM customer WHERE id = ?", req.params.id, function(err, result, fields)
- {
- // if (err) throw err;
- res.send(result)
- });
- });
- app.post('/customers', function(req, res)
- {
- var customer = {
- name: req.body.name
- }
- con.query("INSERT INTO customer SET ?", customer, function(err, result)
- {
- // if (err) throw err;
- console.log(err);
- res.send(customer)
- });
- })
- app.put('/customers/:id', function(req, res) {
- var customer = {
- name: req.body.name
- }
- con.query("UPDATE customer SET ? WHERE id = ?", customer, req.params.id, function(err, result)
- {
- // if (err) throw err;
- res.send(customer)
- });
- })
- app.listen(3000);
- app.on("end", () => {
- client.close();
- });
- });
Add Comment
Please, Sign In to add comment