supertokens / supertokens-node

Node SDK for SuperTokens core
https://supertokens.com
Other
292 stars 79 forks source link

Express - Auth routes are giving 404 #779

Closed sarathjasrin closed 9 months ago

sarathjasrin commented 9 months ago

I have tried to create a simple express node and next js front end. The front end is created using started pack module.

Backed server.js

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const supertokens = require('supertokens-node');
const { middleware } = require('supertokens-node/framework/express');
const { errorHandler } = require('supertokens-node/framework/express');
const { superInit } = require('./supertoken');

const app = express();
const port = 3000;

superInit(); // contains supertoken init function

app.use(cors(
    {
        origin: "http://localhost:3001",
        allowedHeaders: ["content-type", ...supertokens.getAllCORSHeaders()],
        credentials: true
    }
));
app.use(bodyParser.json());
app.use('/api',middleware());
app.use(errorHandler());

app.get('/ping', (req, res) => {
    res.send('pong');
});

app.listen(port, () => {
    console.log(`superSimon app listening at http://localhost:${port}`);
});

The api throwing 404

Screenshot 2567-01-07 at 10 24 05 PM

Node : v20.6.0

rishabhpoddar commented 9 months ago

Hey! You need to use app.use(middleware()), instead of app.use('/api',middleware());.

Also, in supertokens.init, you need to set apiBasePath to /api/auth in the appInfo config if you want to use /api/auth as the prefix instead of /auth (which is the default)