Using routes from another file

I put my routes in a separate folder but its not working on my server. how do i fix this?

server.ts

import * as express from 'express';
import * as https from 'https';
import { routes } from './routes/routes';

const app = express();

app.listen(8080, () => {
    console.log('listening on 8080');
});

routes('/');

./routes/routes

import * as express from 'express';
const app = express();

export const routes = function(url) {
    app.get(url, (req, res) => {
        res.send('ready');
    });
};

Did you store your routes in a .js file?

stored in .ts.
would like to keep all files in src folder as ts files

this is my error in the browser
Cannot GET /

Maybe export default?

No, sorry, not relevant.

So if they’re in the same file with that structure, is it working ?

I think the code separation isn’t the problem, since you’re already getting Cannot GET / which means the code in the routes file is being executed…

Those are two different instances of app().
You should pass app to routes and use it there instead of importing another express.

1 Like