So… I have wayyyy too many routes in my main file, and I was wondering if I could make a folder named routes and put all of my routes in a js file then require them in another file then require that file in my main index.js.
oof
So… I have wayyyy too many routes in my main file, and I was wondering if I could make a folder named routes and put all of my routes in a js file then require them in another file then require that file in my main index.js.
oof
require("/file.js");
There are useful examples of organising routing in the express docs, you’ll also likely find the parameter section useful and the static files section …
http://expressjs.com/en/starter/static-files.html
http://expressjs.com/en/guide/routing.html
http://expressjs.com/en/guide/writing-middleware.html
http://expressjs.com/en/guide/using-middleware.html
What I do is, I make a folder called Routes
And put each category into different files. Each file would have a module.exports
That is a function. All I have to do is use require(folder/file-path-here.js)(app, some-packages)
and I am done. If you need help, feel free to DM me.
This is exactly what I’m doing! Other people have the right idea; put them into separate files. But another thing I did was put all my imports ("const express = require(“express”), whatever) into a separate “imports.js” file, and then export them as a module.exports object.
As for each path (app.get"/", app.get"/posts",) I wrapped them in a function, exported the function and then imported the file and called the function in my main file.
Hope this isn’t too confusing!
btw, is that Atom?
lmao, it’s vscode with a atom theme. Also, very confusing lol.
vscode: hello I am atom competitor
atom: hello I am VScode competitor
@HK420: I have atom theme on VScode