Different classses
Wednesday, 2 November 2022 9:26 AM
[Link]:
import dotenv from 'dotenv';
import {app} from './src/app';
[Link]();
const port = [Link] || 8000;
// Server
[Link](port, () => {
[Link](`Listening on: [Link]
});
[Link]:
import awsServerlessExpress from 'aws-serverless-express';
import { app } from './src/app';
const server = [Link](app)
export const handler = (event:any, context:any) =>
[Link](server, event, context);
[Link]('just to confirm this is exported');
[Link]:
{
"name": "express_in_aws",
"version": "1.0.0",
"description": "",
"main": "[Link]",
"scripts": {
"build": "npx tsc",
"start": "node dist/[Link]",
"dev": "concurrently \"npx tsc --watch\" \"nodemon -q dist/[Link]\"",
"deploy": "serverless deploy"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aws-serverless-express": "^3.3.6",
"dotenv": "^16.0.3",
"express": "^4.18.2",
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aws-serverless-express": "^3.3.6",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"pg": "^8.8.0"
},
"devDependencies": {
"@types/aws-serverless-express": "^3.3.5",
"@types/express": "^4.17.14",
"@types/node": "^18.11.9",
"@types/pg": "^8.6.5",
"concurrently": "^7.5.0",
"nodemon": "^2.0.20",
"serverless": "^3.23.0",
"typescript": "^4.8.4"
}
}
[Link]
service: expressInAWSDemo #Name of your App
provider:
name: aws
runtime: nodejs14.x # Node JS version
memorySize: 512
timeout: 15
stage: production
region: ap-south-1 # AWS region
package:
exclude:
- ./**.ts
- ./**.js
- src/**
- .env
- ./**.json
include:
- dist/**
- node_modules/**
functions:
api:
handler: dist/[Link]
events:
- http: ANY /{proxy+}
- http: ANY /
[Link]
{
"compilerOptions": {
"target": "es2016",
"esModuleInterop": true
"module": "commonjs",
[Link]
{
"compilerOptions": {
"target": "es2016",
"esModuleInterop": true
"module": "commonjs",
"outDir": "./dist",
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
[Link]
import express, { Express, Request, Response } from 'express';
import { Client, ClientConfig } from 'pg';
import { lookupRouter } from './Routes/lookupRoutes';
const app = express();
const dbConfig:ClientConfig = {
host: '[Link]',
user: 'postgres',
password: 'postgres',
port: 5432,
database: 'initial',
};
const client = new Client(dbConfig);
//postgre user - postgres
//postgre password - EuRMaBwBmDqaiD0DgQQy
[Link]([Link]());
[Link]([Link]({extended:false}))
//GET
[Link]('/', (req, res) => {
[Link](200).send('hello world!');
});
//GET req Simply sends the current time
[Link]('/time', (req, res) => {
let timeNow = new Date([Link]());
[Link](200).send([Link]());
});
[Link]('/pgtest', async (req,res) => {
try {
await [Link]();
const res1 = await [Link]("SELECT * FROM public.
\"your_Table\"");
await [Link]();
//callback(null, [Link]);
[Link](200).send([Link]);
} catch (err) {
await [Link]();
//callback(err)
[Link](500).send(err);
\"your_Table\"");
await [Link]();
//callback(null, [Link]);
[Link](200).send([Link]);
} catch (err) {
await [Link]();
//callback(err)
[Link](500).send(err);
}
});
//POST req logs the name and sends it
//To check send a POST req with "name" and check your lambda
function console
[Link]('/logthis', (req, res) => {
const name = [Link];
const toLog = `\n >>> My name is ${name} <<< \n`
[Link](toLog)
[Link](200).send(toLog);
});
[Link]('/lookup', lookupRouter);
export { app }
Routes/[Link]
import express from 'express';
const lookupRouter = [Link]();
//parent route for lookup
[Link]('/')
.get((req,res) => {
var retObject = {
result:
{
region: [{
id:1,
name: 'India'
},
{
id:2,
name: 'US'
}],
location: [{
id:1,
name: 'Chennai'
},{
id:2,
name: 'Hartford'
}]
}
}
[Link]('get method for lookup');
[Link](200).send(retObject);
})
.post((req,res) => {
[Link](200).send('Not Implemented!!');
})
export {lookupRouter}
})
.post((req,res) => {
[Link](200).send('Not Implemented!!');
})
export {lookupRouter}