Я звоню как: http://localhost:3000/data/true/11/22. Моя проблема в том, что когда я отлаживаю с узлом, в первой строке у меня прерывание на разрыв, и я вижу значение req: Request, но шаг за шагом я хочуприсвоить значение req.params.cultura одной переменной и отследить его в консоли не определено.
значения:
req.params.esProduccio: true req.params.cultura: 11 req.params.coleccio: 22 * 1008 *
что я не понимаю, так это то, что я определилроутер как /: cultura /: coleccio /: esProduccio , но мне нужно позвонить http://localhost:3000/data/true/11/22, так что меня смущает, потому что я думаю, что он должен распознавать http://localhost:3000/data/11/22/true
Может быть, это что-то из конфигурации, которую я должен написать.
Я не знаю почему, и я не понимаю, потому что я новичок в наборе текста и выражаю
Можете ли вы помочь мнепожалуйста?
У меня есть этот контроллер данных:
import { Router } from "express";
import { NextFunction } from "express-serve-static-core";
import { Request, Response } from 'express';
import { BLL_Base } from '../_bll/base.bll';
import { ExperienciaLaboral } from "../pocos/experiencialaboral.poco";
import { Client } from "../pocos/client.pocol";
import { Coneixements } from "../pocos/coneixements.poco";
import { Reglada } from "../pocos/reglada.poco";
import { NoReglada } from "../pocos/noreglada.poco";
import { Resposta } from "../pocos/resposta.poco";
import { Idiomes } from "../pocos/idiomes.poco";
import { Capcalera } from '../pocos/capcalera.poco';
export class DataController
{
public router: Router
/**
* Initialize the HeroRouter
*/
constructor() {
this.router = Router();
this.init();
}
/**
* GET by Id.
*
* @param req
* @param res
* @param next
*/
public getData(req: Request, res: Response, next: NextFunction): void {
let resp = new Resposta();
let cultura = req.params.cultura;
let coleccio = req.params.coleccio;
let esProduccio = <boolean> req.params.esProduccio;
try
{
// here the cultura, coleccio and esProduccio is undefined but I have data in the collection
resp.ok = true;
resp.data = res;
}
catch(ex)
{
resp.ok = false;
resp.data = ex.message;
}
res.status(200).send(resp);
}
/**
* Take each handler, and attach to one of the Express.Router's
* endpoints.
*/
init() {
this.router.get('/', this.getAll);
this.router.get('/:cultura/:coleccio/:esProduccio', this.getData);
}
}
У меня есть следующие app.ts:
import * as express from 'express'
import { DataController } from './controllers/data-controller';
import { Router } from 'express';
class App {
public express = express();
public data: DataController =new DataController();
constructor () {
this.express = express()
this.mountRoutes()
}
private mountRoutes (): void {
const router = express.Router()
router.get('/', (req, res) => {
res.json({
message: 'Hello World!'
})
})
this.express.use('/', router)
this.express.use('/data', this.data.router)
}
}
export default new App().express
Я использую tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"allowJs": true,
"outDir": "./bin",
"sourceMap": true,
"noImplicitAny": false,
"strictNullChecks": false
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
А мои пакеты:
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "gulp scripts",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ts-node ./lib/server.ts",
"start": "ts-node src/index.ts",
"debug": "node --nolazy --inspect-brk=9229 myProgram.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/body-parser": "0.0.33",
"@types/chai": "^3.4.34",
"@types/chai-http": "0.0.29",
"@types/debug": "0.0.29",
"@types/express": "^4.16.0",
"@types/mocha": "^2.2.32",
"@types/morgan": "^1.7.32",
"@types/node": "^6.0.46",
"browserify": "^16.2.2",
"chai": "^3.5.0",
"chai-http": "^3.0.0",
"gulp": "^3.9.1",
"gulp-typescript": "^3.1.1",
"mocha": "^3.1.2",
"mongodb": "^3.1.6",
"ts-node": "^3.3.0",
"tsify": "^4.0.0",
"typescript": "^2.0.6",
"vinyl-source-stream": "^2.0.0"
},
"dependencies": {
"@types/cors": "^2.8.4",
"@types/es6-promise": "^3.3.0",
"@types/express-serve-static-core": "^4.0.49",
"@types/mongodb": "^3.1.6",
"body-parser": "^1.18.3",
"debug": "^2.2.0",
"express": "^4.16.3",
"express-session": "^1.15.6",
"linq-es2015": "^2.4.33",
"morgan": "^1.7.0",
"nodemon": "^1.18.4",
"npm-cli": "^0.1.0",
"promise-polyfill": "8.1.0",
"server": "^1.0.18",
"webpack": "^4.16.5",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
}
}
и мой typings.json:
{
"globalDependencies":
{
"core-js": "registry:dt/core-js#0.0.0+20160317120654",
"jasmine": "registry:dt/jasmine#2.2.0+20160505161446",
"node": "registry:dt/node#4.0.0+20160509154515",
"es6-promise": "registry:dt/es6-promise#0.0.0+20160726191732"
}
}
мой файл запуска:
{
"type": "node",
"request": "launch",
"name": "START",
"preLaunchTask": "typescript",
"program": "${workspaceFolder}/src/index.ts",
"cwd": "${workspaceFolder}",
"protocol": "inspector",
"outFiles": [
"${workspaceFolder}/bin/**/*.js"
]
}