Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

learning: working with nitro server #78

Open
Drew-Macgibbon opened this issue Jun 30, 2023 · 2 comments
Open

learning: working with nitro server #78

Drew-Macgibbon opened this issue Jun 30, 2023 · 2 comments
Assignees
Labels
🗣️ discuss further discussion is required

Comments

@Drew-Macgibbon
Copy link
Contributor

Drew-Macgibbon commented Jun 30, 2023

I am starting to see similarities with express, but I still need to dig deeper to understand how things work and how I can better structure/optimize the code. This will be a running thread of my findings.

I'm breaking down my findings into separate issue threads, each thread will become an article/video.

  1. h3event: learning: H3Event object #80
  2. middleware: learning: nitro middleware #81
  3. utils: learning: nitro utils #82
  4. func - unstorage: learning: unstorage #79
@Drew-Macgibbon Drew-Macgibbon self-assigned this Jun 30, 2023
@Drew-Macgibbon Drew-Macgibbon added the 🗣️ discuss further discussion is required label Jun 30, 2023
@Drew-Macgibbon
Copy link
Contributor Author

We can use any express package like so:

// middleware/cors.ts
import cors from 'cors';

export default defineEventHandler(({ req, res }) => new Promise((resolve) => cors()(req, res, resolve)));

Or helmet or any express package:

// middleware/helmet.ts
import helmet from 'helmet';

export default defineEventHandler(({ req, res }) => new Promise((resolve) => helmet()(req, res, resolve)));

@Drew-Macgibbon
Copy link
Contributor Author

Auto-imports

These work the same as client-side auto-imports within your nuxt 3 app.

Example:
directory structure.

server/utils/logger.ts
// preference: I like to name files in sub-folders with the folder as a prefix
server/utils/scraper/scraperBlogs.ts

You can now call logger or scraperBlogs directly if they have a default export.
Named exports are also directly callable.
Let's say the scraperBlogs file has a testFunction named export, yo don't have to prefix it when calling, it can be directly referenced as testFunction()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗣️ discuss further discussion is required
Projects
None yet
Development

No branches or pull requests

1 participant