Hello Stack Overflow community,
I'm facing an issue with the Serverless Framework and the serverless-offline plugin where I'm getting a "route not found" error despite having the correct configuration. I've tried various troubleshooting steps but haven't been able to resolve the issue. I'm seeking assistance to identify what might be causing this problem.
Background:
I'm working on a Serverless application using the Serverless Framework and AWS Lambda. I'm using the serverless-offline plugin for local development and testing.
Issue:
I've defined an endpoint /dev/login with the GET method in my serverless.yml configuration, and I'm trying to access it using http://localhost:3000/dev/login
. However, I'm consistently encountering a "route not found" error.
Relevant Details:
- Serverless Framework version: 3.34.0
- serverless-offline plugin version: 3.34.0
- AWS SDK version: 2.x
Steps Taken:
- I have verified that the endpoint path in the serverless.yml configuration matches the route I'm trying to access.
- I have upgraded the Serverless Framework to version 3.34.0 to ensure compatibility with the serverless-offline plugin.
- I have restarted the Serverless Offline process after making changes to the configuration.
- I have cleared browser caches and ensured that the correct URL (http://localhost:3000/dev/login) is being used.
- I have checked the console output of the Serverless Offline process to confirm that the route is listed.
Despite these efforts, the issue persists, and I'm unable to access the endpoint Here is my serverless.yml code.
service: lambda-invoke
plugins:
- serverless-offline
provider:
name: aws
runtime: nodejs14.x
stage: dev
region: us-east-1
functions:
login:
handler: login.handler
events:
- http:
path: /dev/login
method: get
cors: true
Here is my handler.js(login function code)
const AWS = require('aws-sdk');
const jwt = require('jsonwebtoken');
const crypto = require('crypto');
const dynamoDB = new AWS.DynamoDB.DocumentClient();
const SECRET_KEY = 'your-secret-key'; // Replace with your JWT secret key
const TABLE_NAME = 'CryptoPortfolioTracker-user-harisalimughal'; // Replace with your DynamoDB table name
module.exports.login = async (event) => {
try {
const requestData = JSON.parse(event.body);
const username = requestData.username;
const password = requestData.password;
// Retrieve user data from DynamoDB
const params = {
TableName: TABLE_NAME,
Key: { username },
};
const userData = await dynamoDB.get(params).promise();
if (!userData.Item) {
return {
statusCode: 401,
body: JSON.stringify({ message: 'Invalid credentials' }),
};
}
// Hash and compare passwords
const hashedPassword = crypto.createHash('sha256').update(password).digest('hex');
if (hashedPassword !== userData.Item.password) {
return {
statusCode: 401,
body: JSON.stringify({ message: 'Invalid credentials' }),
};
}
// Generate JWT
const tokenPayload = { username: userData.Item.username };
const token = jwt.sign(tokenPayload, SECRET_KEY, { algorithm: 'HS256' });
return {
statusCode: 200,
body: JSON.stringify({ token }),
};
} catch (error) {
console.error('Error:', error);
return {
statusCode: 500,
body: JSON.stringify({ message: 'Internal server error' }),
};
}
};
.