Server template with ton of features.
It's considered good practice to lock the versions of your environment tools, such as Node.js and pnpm. To do this,
you can specify the required versions in the engines
field in package.json
and configure enforcement using the
.npmrc
file in your project's root.
If needed, you can disable strict version checks by setting engine-strict=false
in .npmrc
.
- clone project
git clone https://github.com/jquery-dlya-slabih/nitro-server.git
- install pnpm 10.x version
npm install -g [email protected]
- install deps
pnpm install
- run dev
npm run dev
Example routes:
- get all users http://localhost:3000/api/users
- get user by id http://localhost:3000/api/users/1
- docs http://localhost:3000/_docs
npm run docker:build
npm run docker:start
pnpm install
npm run build
npm run prod
description | command |
---|---|
run dev build with dev server | npm run dev |
run prod build | npm run build |
run prod server (prod build needed) | npm run prod |
run oxlint | npm run lint |
run prettier check | npm run prettier:check |
run prettier write | npm run prettier:write |
run docker image build | npm run docker:build |
start docker container | npm run docker:run |
stop docker container | npm run docker:stop |
API handlers are located in the server/api
folder.
For API handlers:
- use
defineCachedEventHandler
for cached responses, - use
defineEventHandler
for regular uncached handlers.
For more details, see the Nitro documentation:
To inject middleware for APIs or routes, use the server/middleware
directory.
See the Nitro middleware documentation for details.
Nitro plugins will be executed once during server startup to allow extending Nitro's runtime behavior.
They receive nitroApp context, which can be used to hook into Nitro lifecycle events.
Plugins are auto-registered from server/plugins
directory and run synchronously
(by order of file name) on the first Nitro initialization.
Nitro handles assets via the server/public
directory. All assets in directory
will be automatically served. This means that you can access them directly from the browser without any special configuration.
You can define cron jobs in the server/tasks
directory and schedule them in nitro.config.ts
. For help configuring
schedules, check out crontab.guru – a useful tool for cron syntax.
Tasks can be executed in three ways:
- Automatically
- Programmatically
- Manually
Documentation here.
You can add your application specific utils inside server/utils
directory and they will be auto-imported when used.
Every export in the utils directory and its subdirectories will become available globally in your application. Check
documentation for more.
To enable OpenAPI documentation, use defineRouteMeta
in your API handlers. For Swagger or Scalar configuration,
modify the nitro.config.ts
file and read the official documentation.
There is a pre-push
hook in the .githooks
folder. If you want to add another hook, for example pre-commit
, then you need:
- create a file with name
pre-commit
in.githooks
folder - in project root use command
chmod +x .githooks/pre-commit
- fill new hook with any commands, like
pre-push
hook
If you want to delete all hooks:
- remove
.githooks
folder - remove
prepare
command inpackage.json
- in project root use command
git config --unset core.hooksPath
If you encounter a pnpm installation error like: Known issue: Error: Error when performing the request to https://registry.npmjs.org/pnpm/-/pnpm-10.11.0.tgz; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting.
). Try running the docker:build
command again.
Feel free to open an issue or submit PRs.