Gathering detailed insights and metrics for bihongojs
Gathering detailed insights and metrics for bihongojs
Gathering detailed insights and metrics for bihongojs
Gathering detailed insights and metrics for bihongojs
Simple, lightweight abtraction to create Express typescript project.
npm install bihongojs
Typescript
Module System
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
30 Commits
1 Watchers
2 Branches
1 Contributors
Updated on Nov 16, 2022
Latest Version
3.0.6
Package Id
bihongojs@3.0.6
Unpacked Size
174.57 kB
Size
35.73 kB
File Count
191
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Simple, lightweight abtraction to create Express typescript project.
https://github.com/SojebSikder/nodejs-ecommerce
Using yarn :
yarn create sojeb-express-ts-app hello-world
Try changing example.controller.ts
file in app directory.
1@Controller() 2export class ExampleController { 3 @Get("", { middleware: [decorateHtmlResponse()] }) 4 index(req: Request, res: Response) { 5 res.render("index"); 6 } 7}
yarn prisma migrate dev
yarn install
yarn build
yarn start:prod
Run: yarn start
Watch changes: yarn start:dev
Controllers is used to handling requests and reponses. We use classes and decorators to create basic controllers. Decorators associate classes with required metadata to create routing map.
In following example we will we'll use the @Controller()
decorator, which is required to define a basic controller. We'll specify an optional route path prefix of example
. Using a path prefix in a @Controller()
decorator allows us to easily group a set of related routes, and minimize repetitive code. For example, we may choose to group a set of routes that manage interactions with a customer entity under the route /customers. In that case, we could specify the path prefix customers in the @Controller()
decorator so that we don't have to repeat that portion of the path for each route in the file.
1@Controller("/example/") 2export class ExampleController { 3 @Get() 4 index(req: Request, res: Response) { 5 res.send("Hello world"); 6 } 7}
HINT: To create a controller using the CLI, simply execute the
yarn cmd make:controller example
command.
We need to config first to use Local storage
1import { Storage } from "../../system/src"; 2import { Module } from "../../system/src/core/decorator"; 3import { ExampleController } from "./example/example.controller"; 4 5@Module({ 6 imports: [ 7 Storage.config({ 8 driver: "local", 9 connection: { 10 // Set public directory here 11 rootUrl: "public", 12 }, 13 }), 14 ], 15 controllers: [ExampleController], 16}) 17export class AppModule {}
And we are ready to upload files to s3 bucket. To upload files, here is the basic example:
1 // import { Storage } from "../../../system/src"; 2 public async upload() { 3 await Storage.put("sojebdemo/test.txt", "Hello world"); 4 }
We can get url from bucket using Storage.url('fileName')
.
1 // import { Storage } from "../../../system/src"; 2 public async getFileUrl() { 3 return Storage.url("sojebdemo/test.txt"); 4 }
If we want to read files:
1 // import { Storage } from "../../../system/src"; 2 public async getFile() { 3 return Storage.get("sojebdemo/test.txt"); 4 }
Now If we want to delete files:
1 // import { Storage } from "../../../system/src"; 2 public deleteFile() { 3 return Storage.delete("sojebdemo/test.txt"); 4 }
To use aws S3 first we need to config in app.module.ts
:
1import { Storage } from "../../system/src"; 2import { Module } from "../../system/src/core/decorator"; 3import { ExampleController } from "./example/example.controller"; 4 5@Module({ 6 imports: [ 7 Storage.config({ 8 driver: "s3", 9 connection: { 10 awsAccessKeyId: "...", 11 awsSecretAccessKey: "...", 12 awsBucket: "...", 13 awsDefaultRegion: "ap-southeast-1", 14 }, 15 }), 16 ], 17 controllers: [ExampleController], 18}) 19export class AppModule {}
And we are ready to upload files to S3 bucket. To upload files:
1 // import { Storage } from "../../../system/src"; 2 public async upload() { 3 await Storage.put("sojebdemo/test.txt", "Hello world"); 4 }
We can get url from bucket using Storage.url('fileName')
.
1 // import { Storage } from "../../../system/src"; 2 public async getFileUrl() { 3 return Storage.url("sojebdemo/test.txt"); 4 }
If we want to read files from S3 we have to use S3Adapter. And pass Storage config in S3Adapter constructor.
1 // import { Storage } from "../../../system/src"; 2 // import { S3Adapter } from "../../../system/src/core/Disk/drivers/S3Adapter"; 3 public async getFile() { 4 const s3Adapter = await new S3Adapter(Storage.getConfig()).get( 5 "sojebdemo/test.txt" 6 ); 7 8 s3Adapter.on("data", function (data) { 9 console.log(data.toString()); 10 }); 11 }
Now If we want to delete files from S3 we can use Storage class again.
1 // import { Storage } from "../../../system/src"; 2 public deleteFile() { 3 return Storage.delete("sojebdemo/test.txt"); 4 }
For sending email, nodemailer used under the hood.
So first config Mail in app.module.ts
.
Change the app.module.ts like this:
1import { env, Mail } from "../../system/src"; 2import { Module } from "../../system/src/core/decorator"; 3import { ExampleController } from "./example/example.controller"; 4 5@Module({ 6 imports: [ 7 Mail.config({ 8 connection: { 9 host: env("MAIL_HOST"), 10 from: { 11 address: env("MAIL_FROM_ADDRESS", "hello@example.com"), 12 }, 13 secure: false, 14 port: env("MAIL_PORT", 587), 15 username: env("MAIL_USERNAME"), 16 password: env("MAIL_PASSWORD"), 17 }, 18 }), 19 ], 20 controllers: [ExampleController], 21}) 22export class AppModule {}
Now to send a basic email we can do like this:
1 /** 2 * send mail 3 */ 4 public async sendMail() { 5 Mail.to("example@example.com").body("Hello world").send(); 6 }
We can automate process using cmd cli.
yarn cmd make:module Blog
yarn cmd make:controller Blog
yarn cmd make:service Blog
yarn cmd list
If you want to contribute fork the repo, create new branch and make pull request.
If you clone this repo then you have to setup these things manually.
yarn prisma migrate dev
Email: sojebsikder@gmail.com
If you find any problem please create an issue.
No vulnerabilities found.
No security vulnerabilities found.