Skip to content
Snippets Groups Projects
Commit 63145574 authored by Jonny Ngo Luong's avatar Jonny Ngo Luong
Browse files

Issue: set up get/set route for posts (#2)

parent fc0e4ed4
No related branches found
No related tags found
1 merge request!2Resolve "Backend: Annonse opprettelse"
import { Response, Request } from "express";
import example from '../services/example';
export const example1 = async (_: Request, res: Response): Promise<void> => {
try {
//res.status(200).json({"hello":"example1","database": "a"});
res.json(await example.getTest());
} catch (error) {
res.status(400).send("Bad Request");
// res.status(500).send("Internal Server Error");
}
};
export const example2 = async (_: Request, res: Response): Promise<void> => {
try {
res.status(200).json({"hello":"example2"});
} catch (error) {
res.status(400).send("Bad Request");
// res.status(500).send("Internal Server Error");
}
};
import { Response, Request } from "express";
import query from '../services/db_query';
import express from 'express';
const router = express.Router();
router.route('/sendPost').get(async (request: Request, response: Response) => {
try {
response.status(200).send("a");
// response.status(200).json(await query("SELECT * FROM test;"));
} catch (error) {
response.status(400).send("Bad Request");
}
});
router.route('/editPost').get(async (request: Request, response: Response) => {
try {
response.status(200).send("a");
//response.status(200).json(await example.getTest());
} catch (error) {
response.status(400).send("Bad Request");
}
});
export default router;
...@@ -16,7 +16,7 @@ app.use(bodyParser.json()); ...@@ -16,7 +16,7 @@ app.use(bodyParser.json());
// Configuring json response // Configuring json response
app.set("json spaces", 2); app.set("json spaces", 2);
app.use("/", routes); app.use("/api", routes);
/* /*
// create the connection to database // create the connection to database
......
import { example1, example2 } from '../controllers/example'; //import { example1, example2 } from '../controllers/example';
import postController from '../controllers/postController'
import express from 'express'; import express from 'express';
const router = express.Router(); const router = express.Router();
router.get("/", (_, res) => {
res.send("Hello world!");
});
router.get('/test', (req, res) => {
console.log(req,res);
res.send("test!");
});
// routes go brrrrr! // routes go brrrrr!
router.get("/example1", example1); //router.get("/example1", example1);
router.get("/example1/example2", example2); //router.get("/example1/example2", example2);
// Endpoints
router.use("/post", postController);
export default router; export default router;
import db from '../services/db'; import db from '../services/db';
async function getTest(){ export default async function query(query: string){
const data = await db.query('SELECT * FROM test',""); const data = await db.query(query,"");
const meta = {page: 1}; const meta = {page: 1};
return { return {
...@@ -10,6 +10,3 @@ async function getTest(){ ...@@ -10,6 +10,3 @@ async function getTest(){
} }
} }
export default {
getTest
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment