generated from lucien/api-template
Added API endpoints and dockerfile
This commit is contained in:
parent
924e55b4f3
commit
5ddad2ed61
31 changed files with 3885 additions and 942 deletions
28
back/api/users.js
Normal file
28
back/api/users.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
const express = require('express');
|
||||
const sha256 = require("sha256");
|
||||
const { getConnection, getUser, addUser } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/:id', async (req, res) => {
|
||||
const id = req.params.id;
|
||||
const connection = await getConnection();
|
||||
const users = await getUser(connection, id);
|
||||
connection.end();
|
||||
if (users[0]) {
|
||||
res.send(users[0]);
|
||||
} else {
|
||||
res.send('No user found');
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/add', async (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
const connection = await getConnection();
|
||||
const hash = sha256(password);
|
||||
await addUser(connection, username, hash);
|
||||
connection.end();
|
||||
res.send({ message: 'User added' });
|
||||
});
|
||||
|
||||
module.exports = router;
|
Loading…
Add table
Add a link
Reference in a new issue