question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript clarification

See original GitHub issue

Tl;DR: Can’t get the autoPrefix thing to work with TS files & ts-node.

I’ve tried to use autoPrefix two ways with TypeScript files, and I cannot seem to find a way to it to work as expected.

Things I’ve tried

First Attempt

I tried to add the options.prefix to the Autoload call, like so

// server.ts
import * as Fastify from 'fastify';
import * as Autload from 'autoload';
import { join } from 'path';

const fastify = Fastify();
fastify.register(Autoload, {
  dir: join(process.cwd(), 'services'),
  includeTypeScript: true,
  options: {
    prefix: '/api'
  }
}

fastify.listen(3000, '0.0.0.0', (err, address)=> {
  if(err) {
    console.error(err);
    process.exit();
  }
  console.log(`Listening on ${address}`);
})
// services/user.ts
module.exports = (fastify, opts, next)=>{
  fastify.get('/user', (req, reply)=> { reply.send('hi'); });
  next();
}

This only adds in the /user route, and not /api/user

Second Attempt

Basically the same server.ts setup, minus the options object.

// services/user.ts
module.exports = (fastify, opts, next) => {
  fastify.get('/user', (r, reply)=>{reply.send('hi'); })
  next();
}

module.exports.autoPrefix = '/api';

In both cases /api/user is not found, but /user is.

Any tips on how to fix this? I’d be happy to submit a pull request for the documentation once I have a solution.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jhechtfcommented, Jul 31, 2019

Alright, I’ll look into what can be done to resolve the issue – might take me a bit since I’m swamped with my 9-5 right now, and I’m going to have to look into fastify-plugin to determine what’s going on from that end.

0reactions
diego-bettocommented, Aug 25, 2022

@jhechtf it’s not a bug, it’s how fastify works. It’s documented in https://www.fastify.io/docs/latest/Plugins/#route-prefixing-option.

Now we get 404 there

Read more comments on GitHub >

github_iconTop Results From Across the Web

The TypeScript Handbook
The reference section below the handbook in the navigation is built to provide a richer understanding of how a particular part of TypeScript...
Read more >
Clarification on Typescript interface when using generics
I want to save datum to another object node and I need to create type definitions for this object node. May I know...
Read more >
Clarification of Typescript and JavaScript with respect to the ...
In JavaScript Programming, a Set is an object of distinct keys, and mind you the distinct keys can have the same value as...
Read more >
JavaScript/TypeScript: Clarifying Closures - Medium
Closures are nothing more than a fancy term for describing an inherent feature of the JavaScript engine — scope.
Read more >
Clarification on typescript optional property : r/typescript - Reddit
Clarification on typescript optional property. As someone who is still learning the language, I need a little clarification for how optional propertys work, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found