This morning everything was working perfectly, but after restarting my computer, it's not running anymore.
I'm at a loss on how to fix this issue.
[nodemon] starting
ts-node src/index.ts
[nodemon] clean exit - waiting for changes before restart
Index.ts
import "dotenv/config";
import "reflect-metadata";
import express from "express";
import { ApolloServer } from "apollo-server-express";
import { buildSchema } from "type-graphql";
import { UserResolvers } from "./UserResolvers";
import { createConnection } from "typeorm";
import cookieParser from "cookie-parser";
import { verify } from "jsonwebtoken";
import cors from "cors";
import { User } from "./entity/User";
import { sendRefreshToken } from "./sendRefreshToken";
import { createAccessToken, createRefreshToken } from "./auth";
(async () => {
const app = express();
app.use(
cors({
origin: "http://localhost:3000",
credentials: true,
})
);
app.use(cookieParser());
app.get("/", (_req, res) => res.send("hello"));
app.post("/refresh_token", async (req, res) => {
const token = req.cookies.jid;
if (!token) {
return res.send({ ok: false, accessToken: "" });
}
let payload: any = null;
try {
payload = verify(token, process.env.REFRESH_TOKEN_SECRET!);
} catch (err) {
console.log(err);
return res.send({ ok: false, accessToken: "" });
}
// token is valid and
// we can send back an access token
const user = await User.findOne({ uid: payload.userId }.uid);
if (!user) {
return res.send({ ok: false, accessToken: "" });
}
if (user.tokenVersion !== payload.tokenVersion) {
return res.send({ ok: false, accessToken: "" });
}
sendRefreshToken(res, createRefreshToken(user));
return res.send({ ok: true, accessToken: createAccessToken(user) });
});
await createConnection();
const apolloServer = new ApolloServer({
schema: await buildSchema({
resolvers: [UserResolvers],
}),
context: ({ req, res }) => ({ req, res }),
});
apolloServer.applyMiddleware({ app, cors: false });
app.listen(4000, () => {
console.log("express server started");
});
})();
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"esModuleInterop": true,
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"baseUrl": "."
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
package.json
{
"name": "auth-server",
"version": "0.0.1",
"description": "Awesome project developed with TypeORM.",
"dependencies": {
"apollo-server-express": "^3.10.2",
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"express": "^4.18.1",
"express-session": "^1.16.1",
"graphql": "^14.2.1",
"jsonwebtoken": "^8.5.1",
"pg": "^7.10.0",
"reflect-metadata": "^0.1.13",
"ts-node": "^10.9.1",
"type-graphql": "^1.1.1",
"typeorm": "0.2.16",
"typescript": "^4.8.3"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/cookie-parser": "^1.4.1",
"@types/express": "^4.16.1",
"@types/express-session": "^1.15.12",
"@types/graphql": "^14.2.0",
"@types/jsonwebtoken": "^8.3.2",
"@types/node": "^11.13.8",
"ts-node-dev": "^1.0.0-pre.32"
},
"scripts": {
"dev": "nodemon --exec ts-node src/index.ts",
"typeorm": "typeorm-ts-node-commonjs"
}
}