Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with using typescript in server code #112

Closed
hofa77 opened this issue Aug 6, 2019 · 5 comments
Closed

Problem with using typescript in server code #112

hofa77 opened this issue Aug 6, 2019 · 5 comments

Comments

@hofa77
Copy link

hofa77 commented Aug 6, 2019

when I want to use type script as server code I get Error

my .ts code


`
import * as express from 'express';

class PostController {
public router;
public path = '/Post';

constructor() {
    this.router = express.Router();
    this.initRoutes();
}

public initRoutes() {
    this.router.get(this.path, this.getPosts);
}

getPosts(request: express.Request, response: express.Response) {
    response.send('Posts list')
}

}

export default PostController;
`

and when I wun " npm run ssr"
I get this error
can you please help me

`
ERROR in ./ServerCode/post.controller.ts 4:11
Module parse failed: Unexpected token (4:11)
You may need an appropriate loader to handle this file type, currently no loader
s are configured to process this file. See https://webpack.js.org/concepts#loade
rs
|
| class PostController {

public router;

| public path = '/Post';
|
@ ./server.ts 73:0-57 74:21-35
`

@hofa77
Copy link
Author

hofa77 commented Aug 6, 2019

and I am using it in the server.ts like this

`import PostController from './ServerCode/post.controller'

app.use('/postUser', PostController);
`

@sherry9872
Copy link

I am also facing this issue. Any solution for this?

@Jorgen-VikingGod
Copy link

Jorgen-VikingGod commented Aug 14, 2019

I had to import my routes with require instead of import

Can you try:

const PostController = require('./ServerCode/post.controller');

app.use('/postUser', PostController);

@Jorgen-VikingGod
Copy link

btw. you need also to export your class in post.controller.ts

In my route it looks like this:

class Api {
  constructor() {
    this.app = express();
    this.mountRoutes();
  }
  public app;
  // Access this API route using {GET} and {POST} localhost:443/api/...
  private mountRoutes(): void {
    const router = express.Router();
    router.post('/dummy', (req, res) => {
      console.log(req.body);
    });
    module.exports = router;
  }
}

export default new Api().app;

@Gorniv
Copy link
Member

Gorniv commented Aug 15, 2019

I recommendation separate scripts for universal and backend

@Gorniv Gorniv closed this as completed Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants