49 lines
1.7 KiB
JavaScript
49 lines
1.7 KiB
JavaScript
//Libraries
|
|
const express = require('express');
|
|
const multer = require('multer');
|
|
const mysql = require('mysql2');
|
|
const games = require('./model/games');
|
|
const {request, response} = require("express");
|
|
|
|
//Setup defaults for script
|
|
const app = express();
|
|
const upload = multer();
|
|
const port = 8787;
|
|
|
|
//The * in app.* needs to match the method type of the request
|
|
app.get('/games', upload.none(),
|
|
async (request, response) => {
|
|
let result = {};
|
|
try {
|
|
result = await games.getAllGames(request.query);
|
|
} catch (error) {
|
|
return response
|
|
.status(500) //Error code when something goes wrong with the server
|
|
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
|
|
.json({message: 'Something went wrong with the server.'});
|
|
}
|
|
//Default response object
|
|
response
|
|
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
|
|
.json({'data': result});
|
|
});
|
|
|
|
app.post('/games', upload.none(),
|
|
async (request, response) => {
|
|
let result = {};
|
|
try {
|
|
result = await games.addNewGame(request.query);
|
|
} catch (error) {
|
|
return response
|
|
.status(500) //Error code when something goes wrong with the server
|
|
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
|
|
.json({message: 'Something went wrong with the server.'});
|
|
}
|
|
response
|
|
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
|
|
.json({message: 'Game added successfully!'});
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Application listening at http://localhost:${port}`);
|
|
})
|