У меня есть nodejs приложение, написанное на TypeScript . Я использую giuseppe
для создания контроллеров и API.
Вот мой server.ts
import { Giuseppe } from "giuseppe";
import { GiuseppeReqResPlugin } from "giuseppe-reqres-plugin";
import * as bodyparser from "body-parser";
import express = require("express");
import nodeEnvFile = require("node-env-file");
import cookieParser = require("cookie-parser");
/**
* Launch HTTP Server apis.
*/
export class Server {
static startServer() {
// Load configuration file
nodeEnvFile("./.env");
const giusi = new Giuseppe();
const expressApp = giusi.expressApp;
expressApp.use(bodyparser.json());
expressApp.use(bodyparser.urlencoded({ extended: true }));
expressApp.use(cookieParser());
// static files
expressApp.use(express.static("public/web"));
giusi.registerPlugin(new GiuseppeReqResPlugin());
console.log(`Loading controllers from folder: ${process.env.API_FOLDER}`);
giusi.loadControllers(`${process.env.API_FOLDER}/**/*.js`)
.then(() => {
giusi.start(+process.env.SERVER_PORT, "/api/v1/");
console.log(`Server is up and running on port ${process.env.SERVER_PORT}.`);
});
}
}
Server.startServer();
пакет. json
"main": "dist/server.js",
"scripts": {
"clean": "del-cli ./dist/*",
"prestart": "yarn clean && tsc && yarn copyfiles",
"copyfiles": "copyfiles .env dist",
"start": "yarn serve",
"serve": "node dist/server.js",
"start-dev": "yarn prestart && concurrently \"tsc --watch \" \"nodemon dist/server.js\""
},
tsconfig. json
{
"compilerOptions": {
"baseUrl": "./",
"target": "es2015",
"module": "commonjs",
"moduleResolution": "node",
"removeComments": true,
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"pretty": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist",
"typeRoots": [
"node_modules/@types"
]
},
"include": [
"src/**/*"
]
}
и один из файлов контроллера / src / api / n go -сервис. ts
import { Controller, Get } from "giuseppe";
import { NgoFacade } from "../facade/ngo-facade";
@Controller("ngoService")
export class NgoService {
@Get("getNgos")
async getNgos(): Promise<any> {
console.log("Hi");
return await NgoFacade.getNgos();
}
}
При нажатии
localhost: 8080 / api / v1 / ngoService / getNgos показывает
Cannot GET /api/v1/ngoService/getNgos