You are building an Express.js API and need to ensure that the API can only be accessed with a valid authentication token. How would you implement middleware to secure your API?
- Use a middleware function to check the authentication token for each API route and grant access only if the token is valid.
- Implement authentication within each route handler, verifying the token before processing the request.
- Rely on HTTPS encryption to secure the API and avoid using authentication middleware.
- Use a third-party authentication service to secure your API and handle token validation externally.
To secure an Express.js API with authentication, you should create a middleware function that checks the authentication token for each API route and grants access only if the token is valid. Centralizing authentication in middleware ensures consistent security across all routes. The other options are either less secure or less maintainable.
Loading...
Related Quiz
- You are tasked with developing a real-time chat application where low latency and high availability are critical. Which type of database would be the most suitable, and what considerations should you have in mind regarding data consistency and partitioning?
- When implementing JWT, where is the token commonly stored for subsequent requests?
- How can developers specify the version of their package when publishing to the NPM registry?
- You are working on a Node.js project with a team, and you notice that the package-lock.json file is frequently causing merge conflicts. How would you resolve or minimize such conflicts while ensuring consistent dependency resolution?
- How does normalizing database tables impact the Read and Update operations in CRUD?