I'm trying to split my application. Unfortunately it does not work to call the controller. As soon as I comment out the controller and the post method it works to call the server.
route/image.ts
const imageController = require('../controllers/imageController.ts')
module.exports = ({ router }) => {
router
.get('/image', ctx => {
ctx.body = 'Image'
})
.post('/image', imageController.newImage)
}
controller/imageController.ts
import { BaseContext } from 'koa'
export default class imageController {
static newImage = (ctx: BaseContext) => {
// return OK status code
ctx.status = 200
ctx.body = 'Test'
}
}
Error
Unexpected token { 11:44:42
(function (exports, require, module, __filename, __dirname) { import { BaseContext } from 'koa'
^
SyntaxError: Unexpected token {
at new Script (vm.js:84:7)
at createScript (vm.js:264:10)
at Object.runInThisContext (vm.js:312:10)
at Module._compile (internal/modules/cjs/loader.js:684:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Module.require (internal/modules/cjs/loader.js:657:17)
at require (internal/modules/cjs/helpers.js:22:18)
Your error suggest that you are trying to run your typescript files without typescript. The line import { x } from 'y';
is not valid in your node.js version, and given that you are importing a type... typescript would have removed that line.
So make sure you run tsc
and you run your .js
files, not your .ts
files.