express-string-controllers
v0.1.0
Published
Reduce imports with express wrapper
Downloads
5
Maintainers
Readme
express-string-controllers
Reduce imports in your expressjs apps using a controllers wrapper
Installation
Using NPM
npm install express-string-controllers --save
Using Yarn legacy
yarn add express-string-controllers
Quick start
If your folder structure is something like this:
- src
- index.ts
- controllers
- PetsController.ts
index.ts
import express from 'express';
import App, { IExpress } from 'express-string-controllers';
// wrap your express app
const app: IExpress = new App(express());
app.get('/pets', 'PetsController.getPets');
// Replace with your port number
app.listen(4000, () => {
console.log('app started');
});
PetsController.ts
class PetsController {
static async getPets(req, res, next) {
return res.status(200).json({
pets: ['dog', 'cat']
});
}
};
export default PetsController;
You can also dictate your controllers folder path manually:
- src
- index.ts
- custom
- custom_controller
- PetsController.ts
- custom_controller
index.ts
import path from 'node:path';
import express from 'express';
import App, { IExpress } from 'express-string-controllers';
// wrap your express app
const controllersPath = path.resolve(__dirname, './custom/custom_controller');
const app: IExpress = new App(express(), controllersPath);
app.get('/pets', 'PetsController.getPets');
// Replace with your port number
app.listen(4000, () => {
console.log('app started');
});
PetsController.ts
class PetsController {
static async getPets(req, res, next) {
return res.status(200).json({
pets: ['dog', 'cat']
});
}
};
export default PetsController;
For more examples, check the __tests__ folder.
Using nodejs require
./index.js
const path = require('path');
const express = require('express');
const { App } = require("express-string-controllers");
// controller path
const cPath = path.join(__dirname, './controllers');
// init app
const app = new App(express(), cPath);
// define routes
app.get('/ban', 'PetsController.helloWorld');
app.listen(2000, () => { console.log('server started');
});
./controllers/PetsController.js
// default exports
module.exports = class PetsController {
static async helloWorld(req, res, next) {
return res.send('hello world');
}
}