Setup (Node Express Typescript)
- tsc --init (es6, outDir - /dist, rootDir - /src, moduleResolution: node)
- npm init -y
- npm i express
- npm i -D typescript ts-node nodemon @types/node @types/express (dev dependency)
- add these scripts to package.json:
"start": "node dist/app.js",
"dev": "nodemon src/app.ts",
"build": "tsc -p ."
- npm run dev
- For importing few static files in nodejs
app.get('/', (req: Request, res: Response, next: NextFunction) => {
res.sendFile(path.join(__dirname, '../public', 'index.html'));
});
- For multiple static imports in nodejs
app.use(express.static(path.join(__dirname, '../public')))