---
# System prepended metadata

title: Teh 2
tags: [Documentation]

---

---
title: 'Teh 2'
---

Teht 2
===


## Table of Contents

[TOC]

## Rest

Sisälsi kohtuullisen paljon uutta asiaa, rest ei ollut ennestään tuttu.

Koodi
---

```
const express = require('express');
const mysql = require('mysql');

const db = mysql.createConnection({
host :'localhost',
user:'root',
password: '00005',
database: 'db'

});

db.connect((err) =>{
if(err){
    throw(err);
}
console.log('mysql yhdistetty');
});

const app = express();
app.get('/LuoTaulukko', (req, res) => {
    let sql = 'CREATE TABLE registration(id int AUTO_INCREMENT, first VARCHAR(255), last VARCHAR(255), age INT(11), PRIMARY KEY(id))';
    db.query(sql, (err, result) => {
        if(err) throw err;
        console.log(result);
        res.send('Henkilötaulukko luotu');
    });
});


app.get('/addhenk',(req,res) =>{
let henk = {first:'Matti', last:'Teppo', age:11};
let sql = 'INSERT INTO registration SET ?';
let query = db.query(sql,henk,(err,result)=>{
    if(err) throw err;
    console.log(result);
    res.send('Henkilö lisätty');
});
});
app.get('/updatehenk/:id', (req, res) => {
    let newFirst = 'Jeesus';
    let newSecond = 'Brian';
    let newAge = 40;
    let sql = `UPDATE registration SET first = '${newFirst}', last ='${newSecond}', age= '${newAge}' WHERE id = ${req.params.id}`;
    let query = db.query(sql, (err, result) => {
        if(err) throw err;
        console.log(result);
        res.send('Henkilön tiedot päivitetty');
    });
});
app.get('/deleteHenk/:id', (req, res) => {
    let sql = `DELETE FROM registration WHERE id = ${req.params.id}`;
    let query = db.query(sql, (err, result) => {
        if(err) throw err;
        console.log(result);
        res.send('Henkilö poistettu tietokannasta');
    });
});
app.get('/getHenkilot', (req, res) => {
    let sql = 'SELECT * FROM registration';
    let query = db.query(sql, (err, results) => {
        if(err) throw err;
        console.log(results);
        res.send('Henkilöt haettu');
    });
});

app.get('/getHenk/:id', (req, res) => {
    let sql = `SELECT * FROM registration WHERE id = ${req.params.id}`;
    let query = db.query(sql, (err, result) => {
        if(err) throw err;
        console.log(result);
        res.send('Henkilö haettu ');
    });
});


app.listen('3000',() =>{
console.log("Aloitetaan kuuntelu")

});


```

Koodissa henkilön päivitys toimii vain esimerkkinä.



## Appendix and FAQ

:::info
**Find this document incomplete?** Leave a comment!
:::

###### tags: `Documentation`
