Noah Falk
express-typescript
, but you can name it whatever you like. Create a package.json file inside this folder.package.json
file with the following content. We’ll need to change a lot of it later on. But leave it for now.cors
and dotenv
. Cors will help us later on, when we create the API endpoints that need to be accessible from a browser. It will set the necessary headers, so that the browser can interact with our API. The dotenv package allows us to create a .env
file in development to read environment variables from. You might know this standard from other frameworks such as Next.js./src
and add a file called server.ts
inside this folder. The file should look like this. It sets up Express, Cors and a sample response to show the server is up and running.rimraf
and tsc-alias
. These will help us later on, when we need to build our code for production.tsconfig.json
file in our project that we can edit. Paste this into the file, replacing all the previous content:npm run SCRIPT
. Now we’re ready to run it in development mode using npm run dev. You should see feedback in your console. Visit http://localhost:3000
to check if everything is working./api
. Create a new file in the src directory called router.ts
. Add this content:/test
and /another-test
. Both are GET methods and call a handler function inside a controller. We will create the handler functions later. First, return to server.ts
and import the router. We’re using the previously created import alias for this./api
should go the the router we’ve just created.controller.ts
with the two handler methods that return JSON:http://localhost:3000/api/test
or http://localhost:3000/api/another-test
and see the JSON that is being returned.