There appears to be some confusion - at least from my side - about what the Docs say and what the code says. Following the documentation, it states to use the mount object on the ServerSettings, but within code this is showing as deprecated and to use other bits instead. Similliarly using ServerLoad.boostrap, etc.
Is there a plan to update the docs to match this or is this prep work for v6?
I love the approach taken by TsEd but it's just a bit confusing in my opinion. Any advise would be beneficial, thanks
I'm sorry about confusing doc. Can you give the link/page where the doc is confusing exactly. I try to fix it ASAP?
My apologize again :)
Romain
No problem at all. It's let about the Docs are confusing but it's the combination of following the docs and then seeing the code
For example, http://tsed.io/getting-started.html#create-your-express-server states using @ServerSettings (which seems fine) but when implementing this, my IDE complains that the "mount" and "componentsScan" are deprecated and to use alternative options such as addControllers and to use ServerLoader.bootstrap
For the mount part, this page in the code states the deprecated tag:
haaa ok. I deprecated mount and componentScan by error. Only method on ServerLoader (mount and componentScan) are deprecated.
I'll revert this.
mount and componentScan (with ServerSettings decorator) are currently the right way to register controller and provider. In haven't other satisfying way or plan to configure Controller and Provider.
Thanks a lot for your feedback ;)
If you found other confusing stuff in our documentation, tel me :)
Romain
I like the way you have it, but only as information another way would be like Symfony or even TypeORM with a configuration file:
ormconfig.yml
https://typeorm.io/#/using-ormconfig/using-ormconfigyml
default:
name: default
type: mysql
database: test
host: localhost
port: 3306
username: root
password: pass__123
entities: [ dist/Entity/**/*.js ]
subscribers: [ dist/Subscriber/**/*.js ]
migrations: [ dist/Migration/**/*.js ]
cli:
migrationsDir: src/Migration
entitiesDir: src/Entity
subscribersDir: src/Subscriber
synchronize: true
logging: false
tsed.yml | tsed.js | tsed.json
uploadDir: 'public/uploads'
httpPort: 127.0.0.1:3000
httpsPort: 127.0.0.1:8000
acceptMimes: ['application/json']
debug: false,
validationModelStrict: true
viewsDir: './views'
mount: ['src/Controller/**/*.ts']
Also with in a configuration file would be cleaner and if someone wants to overwrite can do it from the Server class with this..
Great, that's good to know. I'll implement using the decorator. Thanks for your feedback, appreciate it
@chiqui3d It could be possible. I never directed this part on how to load a configuration from a file. Today the configuration of the decorator can be loaded via any mechanism:
Example1:
import * as config from "./default_config.json"
@ServerSettings(config)
Example2 (now with bootstrap):
import * as config from "./default_config.json"
import {Server} from "./Server";
async function bootstrap() {
const server = await ServerLoader.bootstrap(Server, config)
await server.listen()
}
With node-config module, load configuration is simple also:
import {$log, ServerLoader} from "@tsed/common";
import {Server} from "./Server";
process.env["NODE_CONFIG_DIR"] = __dirname + "/../config";
const config = require("config");
async function bootstrap() {
try {
$log.debug("Start server...");
const server = await ServerLoader.bootstrap(Server, config /* or config.util.toObject() */);
await server.listen();
$log.debug("Server initialized");
} catch (er) {
$log.error(er);
}
}
bootstrap();
Awesome @Romakita , I thank you enormously for the examples, I begin to love this as I do with Symfony. Thank you.
@chiqui3d In the interest of making the app portable, I would also like to suggest looking at using Environment Variables. You could - in the development process - use dotenv to load from a file but in a deployed environment use environment variables
This is in line with https://12factor.net/config so that any config changes don't require a code change but can be changed without. modifying the code making it portable between environments.
For this, I tend to use ts-node -r dotenv/config src/index.ts. The important bit being loading dotenv/config before running the script to import environment variables from the .env file and having dotenv as a devDependencie in package.json
Hope that makes sense
Yes dotenv is also a good alternative to load configuration from any .env files. Given example work with any third party if the module provide a way to retrieve the entire config in a javascript object.
import {$log, ServerLoader} from "@tsed/common";
import {Server} from "./Server";
const config = require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })
async function bootstrap() {
try {
$log.debug("Start server...");
const server = await ServerLoader.bootstrap(Server, config);
await server.listen();
$log.debug("Server initialized");
} catch (er) {
$log.error(er);
}
}
bootstrap();
Most helpful comment
@chiqui3d In the interest of making the app portable, I would also like to suggest looking at using Environment Variables. You could - in the development process - use dotenv to load from a file but in a deployed environment use environment variables
This is in line with https://12factor.net/config so that any config changes don't require a code change but can be changed without. modifying the code making it portable between environments.
For this, I tend to use ts-node -r dotenv/config src/index.ts. The important bit being loading dotenv/config before running the script to import environment variables from the .env file and having dotenv as a devDependencie in package.json
Hope that makes sense