#Can I place the api directory in my project under the src directory instead of app ?
21 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
When creating the project you will be asked if you want to use the app/ dir or the src/ dir (with the app router). Just choose src/ dir and you should be good to go 👍
I have already selected src dir, but it is not stated in the documentation that the api directory can be under src
So since you said that, does that mean I can do it ?
yea
no. route handlers are part of the app router and hence must be placed inside the app router.
though if you host on vercel, you can use a top-level /api folder for serverless functions https://vercel.com/docs/functions
One of you says yes, the other says no, I'm confused who to believe 😄 @onyx hull @dapper flax
then it would make sense to merge all directories (components, lib etc.) under the app directory , I think this is very confusing for a beginner.
yea, they need to be still placed in app/ dir. As far as I know the question while creating the project is just where the app/ dir is. Like is it at /app/ or is it at /src/app/. jourlev, correct me if I am wrong
If you have such an option, why is the src directory recommended by default ?
is it?
thing is, components and lib and so on are not part of the app router. they are merely the things that the app router imports.
route handlers on the other hand are part of the router and must be placed inside the router.
of course you can always make an api folder outside the app router, write all the logic there, then inside the route.ts files import those things.
you also have options like trpc.
well, so that you can store things outside the src directory too. such as assets, scripts, misc files, so on and so forth
but it's just a matter of preference; as far as nextjs is concerned whether or not you use src doesn't matter.
Frankly, this struck me as very strange. For some reason it seems more logical to me to do it the way I said, but you know.
i understand the question as whether they can use src/api instead of src/app/api to which the answer is as i said above
well, whether it makes sense for you or not, you have to get used to it. perhaps i am already used to it so it makes sense to me
yes like that , I think it would be nice if we could use the API directory directly under src, or if such an opportunity were given and optionally specified in the documents, for example.
Different perceptions