A REST API for a fictional online store coded while following André Baltieri's tutorial from balta.io.
After finishing it, I decided to convert it to Typescript for additional practice.
JavascriptTypescript- Node.js
- Nodemon
- Express.js
- Mongoose (for MongoDB)
- jsonwebtoken
- MongoDB
- Authentication/Authorization using JWT
- Sending e-mail through SendGrid service
- Storage for images using Azure Storage service
- Applied patterns (Model, Controller, Repository, Service)
Criando APIs com NodeJs (Brazilian portuguese)
How to Convert Node.js Code from JavaScript to TypeScript
- Node.js must be installed
- You need a MongoDB server running locally or on a provider like MongoDB Atlas. Edit the connection string on
config.ts
to point to your server.
Install dependencies with:
npm install
Copy file config.template.ts
to config.ts
and edit values.
To enable cloud storage on Azure and e-mailing through SendGrid you need to enter API keys on the config file.
If deploying on a cloud service provider, it is recommended to grab values from the environment. Change config.ts
to something like this:
module.exports = {
connectionString: process.env.MONGODB_CONNSTR,
sendgridKey: process.env.SENDGRID_API_KEY,
sendgridSender: process.env.SENDGRID_SENDER,
azureContainerConnectString: process.env.AZURE_CONNSTR
};
And create the environment variables accordingly.
To run locally, type in a terminal at the project's root folder:
npm start
To execute the server while watching code changes, use:
npx nodemon -r ts-node/register/transpile-only src/server.js
To debug inside Visual Studio Code, press F5
or go to menu Run > Start debugging
.
/
├───.vscode Configuration for Visual Studio Code
├───node_modules Installed module dependencies
├───out Output dir for transpiled javascript
└───src Source code for server
|
│ server.ts Server startup code
│ app.ts Main application code
│ config.ts Configuration
│
├───models Model definitions for mongoose/MongoDB
├───repositories Data repository
├───controllers Controller implementation
├───routes Routing configuration
├───services Services for storage, mailing and auth
└───validators Data validation utilities