I am running an express server that has some REST API
endpoints including authentication endpoint: http://localhost:3000/login
using jwt
(json web token), the server run but when I used postman and several other methods to call the endpoint I keep waiting for the results.
I created an express server in the nodejs
environment like so :
require("dotenv").config()const express = require('express')const app = express()const jwt = require('jsonwebtoken')app.use(express.json)const posts = [ { username: "kyle", name: "post1" }, { username: "Tyler", name: "post2" },]const authenticateToken = (request, response, next) => { const authHeader = request.headers['authorization'] const token = authHeader && authHeader.split('')[1] if(token === null) { return response.sendStatus(401)} jwt.verify(token , process.env.ACCESS_TOKEN_SECRET_KEY, (err, user) => { if(err) return response.sendStatus(403) request.user = user next() })}app.get("/posts", authenticateToken, (request, response) => { const filteredPosts = posts.filter(post => post.username === request.user.username) response.json(filteredPosts)})app.post("/login", (request, response) => { //authenticate the user const username = request.body.username const user = {username: username} const accessToken = jwt.sign(user, process.env.ACCESS_TOKEN_KEY) response.json({accessToken: accessToken})})app.listen(3000, () => { console.log(`app is lestening on port: 3000`)})
I run the server successfully using the command : node server.js
and use postman like so :postman dashboard where the call is made
I expected the request to be treated and I will have the access token as a response, but the request kept pending, I tried using multiple ways to call the api endpoint and I tried to update the nodejs
version and creating another project but this didn't work
the rest client vscode approach
here is my package.json
file:
{"name": "api","version": "1.0.0","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1" },"author": "","license": "ISC","description": "","dependencies": {"dotenv": "^16.4.5","express": "^4.19.2","jsonwebtoken": "^9.0.2" }}