Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warmup done #234

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions nodejs/week3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.vscode/
.vscode/
.vscode/
.vscode/
**/.vscode/
node_modules/
.vscode/
.dockerignore
12 changes: 12 additions & 0 deletions nodejs/week3/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:16


WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

CMD ["npm", "run", "dev"]
59 changes: 59 additions & 0 deletions nodejs/week3/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import express from 'express';
import knex from 'knex';

const knexInstance = knex({
client: 'mysql2',
connection: {
host: process.env.DB_HOST || '127.0.0.1',
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'my-secret-pw',
database: process.env.DB_NAME || 'hyf_node_week3_warmup',
multipleStatements: true,
},
});

const app = express();
const port = process.env.PORT || 3000;

app.use(express.json());

const apiRouter = express.Router();
app.use('/api', apiRouter);

const contactsAPIRouter = express.Router(); // Sørg for, at routeren er defineret her
apiRouter.use('/contacts', contactsAPIRouter);

contactsAPIRouter.get('/', async (req, res) => {
let query = knexInstance.select('*').from('contacts');

const allowedSortFields = ['first_name', 'last_name'];
const allowedSortOrders = ['ASC', 'DESC'];

if ('sort' in req.query) {
const sortParam = req.query.sort.toString();
const sortParts = sortParam.split(' ');
const column = sortParts[0];
const order = sortParts[1] ? sortParts[1].toUpperCase() : 'ASC';

if (allowedSortFields.includes(column) && allowedSortOrders.includes(order)) {
query = query.orderBy(column, order);
} else {
return res.status(400).json({ error: 'Invalid sort parameter' });
}
}

console.log('SQL', query.toSQL().sql);

try {
const data = await query;
res.json({ data });
} catch (e) {
console.error(e);
res.status(500).json({ error: 'Internal server error' });
}
});

app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
27 changes: 27 additions & 0 deletions nodejs/week3/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3.8'

services:
mysql:
image: mysql:8
container_name: mysql-container
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw
MYSQL_DATABASE: hyf_node_week3_warmup
networks:
- mynetwork
ports:
- "3306:3306"

node-app:
build: .
container_name: node-app-container
depends_on:
- mysql
networks:
- mynetwork
ports:
- "4000:3000"

networks:
mynetwork:
driver: bridge
Loading