7. Typescript Journey - Express Webserver
Goal
Run a webserver that can serve up an API written in Typescript
Setup
- Start with clean repo that contains devcontainer setup Source Code.
- Create new branch
- Add postStartCommand to devcontainer.json. It should look like this.
{
"build": {"dockerfile": "Dockerfile"},
"customizations": {
"vscode": {
"extensions": [
"dbaeumer.vscode-eslint",
"kakumei.ts-debug"
]
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [3000],
"postStartCommand": "npm install"
}
- Add install of nodemon to dockerfile
ARG VARIANT=18-buster FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:${VARIANT} RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ && apt-get -y install git RUN npm install -g nodemon
Create Express App
- Initialize App and accept all the defaults
npm init
- Change main to point to dist/index.js. This will be where your transcompiled typescript puts javascript file that will be served by express.
Add dependencies and devDependencies to package.json
Your package.json file should look like this:
{ "name": "typescriptjourney", "version": "1.0.0", "description": "Code Repo for Journey to learn Typescript", "main": "dist/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "git+https://github.com/two4suited/TypescriptJourney.git" }, "author": "", "license": "ISC", "devDependencies": { "@types/express": "^4.17.14", "typescript": "^4.8.4" }, "dependencies": { "express": "^4.18.2" }, "bugs": { "url": "https://github.com/two4suited/TypescriptJourney/issues" }, "homepage": "https://github.com/two4suited/TypescriptJourney#readme" }
- Restart your dev container.
- Create dist folder and index.ts file
mkdir dist touch index.ts
- Initialize Typescript. This will create a tsconfig.json which we will modify in the next step to point to the directory with the ts files and where to put the javascript files
npx tsc --init
- We will uncomment out rootDir and outDir. We will then change our outDir to point to the dist folder. Here is the complete tsconfig.json. I removed the comment out lines for space.
{ "compilerOptions": { "target": "es2016", "module": "commonjs", "rootDir": "./", "outDir": "./dist", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true } }
- Start the typesript compiler
npx tsc --watch
- Add the following code to your index.ts file to create an express webserver
import express from "express";
const app =express();
app.get("/",(req,res) => {
res.send("Hello from your first Express app")
});
const port = process.env.PORT || 3000
app.listen(port,() => console.log(`App listening on PORT ${port}`));
- Add start and dev sections to the package.json file under scripts
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node dist/index.js", "dev": "tsc -w & nodemon dist/index.js" }
- Run the app
npm run dev