https://github.com/probablyarth/typescript-nodejs-zod
template repository for projects using typescript and nodejs.
https://github.com/probablyarth/typescript-nodejs-zod
Last synced: 12 months ago
JSON representation
template repository for projects using typescript and nodejs.
- Host: GitHub
- URL: https://github.com/probablyarth/typescript-nodejs-zod
- Owner: probablyArth
- Created: 2024-04-16T21:22:23.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-11T22:44:27.000Z (almost 2 years ago)
- Last Synced: 2025-03-06T16:23:30.410Z (over 1 year ago)
- Language: TypeScript
- Size: 161 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# server
Template for express, typescript, zod projects.
# Glossary
- ## Absolute imports
non-library imports that use absolute path instead of relative.
for ex:
```
.
├── apps
│ └── server.ts
├── env
│ └── index.ts
├── folder1
│ └── folder2
│ └── a.ts
├── index.ts
└── middlewares
└── logger.middleware.ts
```
```typescript
// folder1/folder2/a.ts
// this is a relative import
import { getEnv } from '../../env/index.ts';
// this is an absolute import
import { getEnv } from 'env/index.ts';
```
# Features
- support for [absolute imports](#absolute-imports) in both development and build using `tscpaths` and `tsconfig-paths`
- `zod` for validating `.env` vars.
- `dotenv` for parsing `.env` files.
- `morgan` for logging.
- `jest` and `supertest` for tests.
- `express` for http server.
- `prettier` for formatting.
- `eslint` for linting and forcing code styles.
# How-to(s)
- ## How to use absolute imports
Absolute imports are not resolved by the default typescript build tool, `tscpaths` takes care of it.
if you want to create imports from a folder let's say `src/folder1` and name it `folder1`
add this entry in the `tsconfig.json`
```jsonc
{
//tsconfig.json
"compilerOptions": {
//...rest of the config
"paths": {
//..other paths
"folder1/*": ["folder1/*"],
},
},
}
```
you don't need to mention `src/folder1/*` because the `baseUrl` is set to `./src` which is changeable in the `tsconfig.json`
```jsonc
//tsconfig.json
{
"compilerOptions": {
"baseUrl": "./src", // <--change here
},
}
```
- ## How to add env vars to the pre-existing zod schema
just add the key in the `envSchema` object in `./src/env/index.ts` and give it the value of `z.string()`
```typescript
// ./src/env/index.ts
.
.
.
.
const envSchema = z.object({
// other variables
yourKey: z.string(),
});
```
## setup
- Install the dependencies
```sh
npm i
```
- create a `.env` file from `.env.example`
```sh
cp .env.example .env
```
## Running the server
- Follow the instructions mentioned in [setup](#setting-up)
- for development server
```sh
npm run dev
```
- for running production build
```sh
npm run build && npm start
```
- using Docker
builidng docker image
```sh
docker build -t .
```
running docker image
```sh
docker run -e PORT=4000 -p 4000:4000
Note: PORT is an env variable
```
## Running tests
```
npm run test
```
## Linting
```sh
npm run lint
```