api_server : Template updated

This commit is contained in:
Rene V. Vergara A. 2023-12-23 20:45:33 -05:00
parent cd9b29365b
commit b080396108

View file

@ -5,20 +5,66 @@
// ----------------------------------------- // -----------------------------------------
import express, {Request,Response,Application} from 'express'; import express, {Request,Response,Application} from 'express';
// ------------------------------------------------------------
// Mock data for testing
// ------------------------------------------------------------
type DType = {
country: string;
city: string;
};
const data : DType[] = [ {country:'United States', city:'Washington'},
{country:'Mexico', city:'Mexico'},
{country:'France', city:'Paris'},
{country:'England', city:'London'}
];
// -------------------------------------------------------------
// Initialize the server
// -------------------------------------------------------------
const app:Application = express(); const app:Application = express();
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
// -------------------------------------------------------------
// Main entry point, URL must be tailored to application requirements // Main entry point, URL must be tailored to application requirements
// -------------------------------------------------------------
app.get('/',(req:Request, resp:Response): void => { app.get('/',(req:Request, resp:Response): void => {
// Get request processed here... // Process main page
resp.send('Main page loaded'); resp.status(200).send('Main page loaded');
}); });
app.post('/', (req:Request, resp:Response): void => { // -------------------------------------------------------------
// Post request processed here... // Process API Cals here
resp.send('Post response is on the way...(maybe...)') // -------------------------------------------------------------
app.get('/data',(req:Request, resp:Response): void => {
// Process GET requests here...
console.log('Body ' + req.query);
resp.status(200).send('Data requested to API -> ' + JSON.stringify(data));
}); });
app.post('/data', (req:Request, resp:Response): void => {
// Process POST requests here...
console.log('Post query' + JSON.stringify(req.query));
resp.status(2send('Post response is on the way...(maybe...)\n' +
JSON.stringify(req.query));
});
// -------------------------------------------------------------
// Catch all requests
// -------------------------------------------------------------
app.get('*', (req:Request, resp:Response): void =>{
// GET not recognized
resp.status(404).send('Page not found!!!');
})
app.post('*', (req:Request, resp:Response): void =>{
// POST not recognized
resp.status(404).send('POST not allowed for requested endpoint!!!');
})
// --------------------------------------------------------------
// Start API Server
// --------------------------------------------------------------
app.listen(PORT, ():void => { app.listen(PORT, ():void => {
console.log('Basic API Server listening on Port:'+PORT+ console.log('Basic API Server listening on Port:'+PORT+
'\nTypeScript Version'+ '\nTypeScript Version'+