Я новичок в использовании socket.io с NodeJS и мокко. После использования клиента и сервера socket.io у меня появляется ошибка
Router.use() requires a middleware function but got a undefined
во время сборки проекта. Ниже приведены файлы и сведения о коде
backend / main.ts
const app = express()
const http = require('http').createServer(app)
...
app.use('/api', require('./routes/api'))
...
export const io = require('socket.io')(http)
io.listen(http)
io.on('connection', (client: SocketIO.Socket) => {
// eslint-disable-next-line no-console
console.log('Socket open Connection')
client.on('disconnect', () => {
// eslint-disable-next-line no-console
console.log('socket disconnect')
})
})
...
backend / routs / api / index.ts
const router = Router()
// Global middleware for all /api routes
router.use('/ping', ping)
router.use('/crm', isLoggedIn, ensureAccessToken, crmSource)
router.use('/app', isLoggedIn, ensureAccessToken, app)
router.use('/app-users', isLoggedIn, ensureAccessToken, ensureIsGlobalTeamMember, appUsers)
router.use('/business', isLoggedIn, ensureAccessToken, ensureIsAdmin, business)
router.use('/app-offers', isLoggedIn, ensureAccessToken, appOffers)
router.use('/app-contracts', isLoggedIn, ensureAccessToken, appContracts)
router.use('/app-order', isLoggedIn, ensureAccessToken, appOrders)
router.use('/app-export', isLoggedIn, ensureAccessToken, appDetailsExport)
router.use('/app-global-settings', isLoggedIn, ensureAccessToken, appGlobalSettings)
if (config.environment === 'local' || config.environment === 'dev') {
router.use('/translations', translationRoutes)
}
// Non-existent API routes should return 404, not the HTML page
router.use((_req, res) => {
res.status(404).end()
})
// export default router
module.exports = router
backend / маршруты / api / crmSource / ts
const router = Router()
router.get('/', (req: ApiRequest, res: Response) => { ... } )
router.get('/source/number/:sourceNumber', (req: ApiRequest, res: Response) => { ... } )
...
export default router
backend / routs / api / appUsers.ts
const router = Router()
router.post(...)
router.delete(...)
export default router
Все остальные промежуточные программы в глобальном промежуточном программном обеспечении маршрутизатора пишутся аналогичным образом.
Когда я импортирую io из main.ts для связи через сокет в следующем файле checkout-offer.ts
import {io as socketIoConnection} from './main'
...
function checkoutOffer(db, logger, offer, token) {
socketIoConnection.emit('saveOffer')
await db.saveCheckoutOffer(offer, logger, token)
socketIoConnection.emit('offerCheckedOut')
return db.getCheckedOffer(db, updatedOffer.id)
}
...
, я получаю ошибку ниже в тестовой сборке с mocha. Реализация сокета без запуска теста mocha прекрасно работает в приложении. Но при запуске npm run test
я получаю эту ошибку.
> myproject@1.0.0 test /Users/<user>/myproject
> npm-run-all --sequential test:unit lint test:api test:browser
> myproject@1.0.0 test:unit /Users/<user>/myproject
> NODE_ICU_DATA=node_modules/full-icu mocha "test/unit/**/*test.{tsx,ts}"
ERROR myproject: Router.use() requires a middleware function but got a undefined
TypeError: Router.use() requires a middleware function but got a undefined
at Function.use (/Users/<user>/myproject/node_modules/express/lib/router/index.js:458:13)
at Object.<anonymous> (/Users/<user>/myproject/backend/routes/api/index.ts:28:8)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/Users/<user>/myproject/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/<user>/myproject/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/Users/<user>/myproject/backend/main.ts:91:17)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/Users/<user>/myproject/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/<user>/myproject/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/Users/<user>/myproject/backend/finalize-offer.ts:18:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/Users/<user>/myproject/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/<user>/myproject/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/Users/<user>/myproject/backend/routes/api/offers.ts:15:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/Users/<user>/myproject/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/<user>/myproject/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/Users/<user>/myproject/test/unit/i18n-test.ts:8:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/Users/<user>/myproject/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/<user>/myproject/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at /Users/<user>/myproject/node_modules/mocha/lib/mocha.js:231:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/Users/<user>/myproject/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/Users/<user>/myproject/node_modules/mocha/lib/mocha.js:536:10)
at Object.<anonymous> (/Users/<user>/myproject/node_modules/mocha/bin/_mocha:573:18)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
пакет. json тестовый скрипт
"test": "npm-run-all --sequential test:unit lint test:api test:browser",
"lint": "eslint --ext .ts,.tsx,.js . && stylelint \"**/*.less\" \"**/*.css\"",
"lint-fix": "eslint --ext .ts,.tsx,.js . --fix && stylelint \"**/*.less\" \"**/*.css\" --fix",
"test:unit": "NODE_ICU_DATA=node_modules/full-icu mocha \"test/unit/**/*test.{tsx,ts}\"",
"test:api": "NODE_ICU_DATA=node_modules/full-icu mocha test/api/**/*.ts",
"test:browser": "NODE_ICU_DATA=node_modules/full-icu mocha test/browser/**/*.ts",
Это промежуточное ПО, в которое он импортируется в backend/routes/api/index.ts
( см. выше) глобальное промежуточное программное обеспечение маршрутизатора router.use('/app-offers', isLoggedIn, ensureAccessToken, appOffers)
бэкэнд / routs / api / appOffers.ts
/* imports */
const router = Router()
const upload = multer()
router.post('/offer/:offerId', (req: ApiRequest, res: Response) => {
...
})
router.get('/:offerId', (req: ApiRequest, res: Response) => {
...
})
router.post('/offer/:offerId/finalize', (req: ApiRequest, res: Response) => {
...
})
export const setActiveOfferAsInCrm = (restUrl: string, accessToken: string, logger: Logger) => async (offers: SavedOffer[]): Promise<SavedOffer[]> => {
...
}
router.post('/:offerId/activate', (req: ApiRequest, res: Response) => {
...
})
router.post('/:offerId/doc-attachment/upload/:type', upload.single('file'), async (req: ApiRequest, res: Response) => {
...
})
router.post('/:offerId/doc-attachment/:attachmentId/delete', async (req: ApiRequest, res: Response) => {
...
})
router.post('/:offerId/cloning', async (req: ApiRequest, res: Response) => {
...
})
router.post('/:offerId/doc-attachment', async (req: ApiRequest, res: Response) => {
...
})
enum AttachmentTypes {
_Docx = 'docx',
_Jpg = 'jpg',
_Pdf = 'pdf',
_Xlsx = 'xlsx',
_Csv = 'csv'
}
type DocumentType = AttachmentTypes._Docx | AttachmentTypes._Jpg | AttachmentTypes._Pdf | AttachmentTypes._Xlsx
export const documentTypes: DocumentType[] = [AttachmentTypes._Docx, AttachmentTypes._Jpg, AttachmentTypes._Pdf, AttachmentTypes._Xlsx]
export function getDocumentFilename(documentType: DocumentType) {
switch (documentType) {
...
}
}
function getEmailAttachmentName(t: TranslationFunction, AttachmentTypes: AttachmentTypes, extension: string): string {
const prefixAndTranslate = (key: string) => t('contractPdf.' + key)
switch (AttachmentTypes) {
...
}
}
function getEmailAdditionalAttachmentName(t: TranslationFunction, AttachmentTypesNumber: number, originalName: string): string {
const prefixAndTranslate = (key: string) => t('contractPdf.' + key)
return prefixAndTranslate('AttachmentTypes') + ' ' + AttachmentTypesNumber + ' - ' + originalName
}
async function loadAndAttachDocument(logger: Logger,
t: TranslationFunction,
businessId: BusinessId,
language: Language,
documentType: DocumentType): Promise<AttachmentFile | undefined> {
...
}
export function getDocumentPath(businessId: BusinessId, language: Language, documentType: DocumentType): string {
return path.join(__dirname, '..', '..', 'assets', 'AttachmentTypeses', businessId, language, getDocumentFilename(documentType))
}
router.post('/:offerId/email', async (req: ApiRequest, res: Response) => {
...
})
export default router