1

I am new to NodeJS and am trying to learn TypeScript.

While following Microsoft's TypeScript-Node-Starter, I have a question about how the app is working.

What I understand:

  1. All ts files under src are compiled into js files under dist.
  2. This is the app's starting point.
  3. The static files are served from the folder src/public.
  4. In this html (or pug) file, the page would try to load main.js script from the folder src/public/js.

Now my question is: how does the html know to load main.js from dist/public/js rather than src/public/js? Am I missing something?

1 Answer 1

1
  1. correct. You can see why it's src/ to dist/ and a few other details regarding TypeScript-to-JavaScript in tsconfig.json (TypeScript expects this file to exist and declare all these things - see here for details).
  2. wrong, but it's kind of tricky. Node here isn't run directly since it doesn't declare an entry point in package.json. Instead, this file declares a script.start command that is called when typing directly npm start, which executes npm run serve, which is declared to execute node dist/server.js. Conclusion: your entry point is server, not app.
  3. wrong: __dirname is a variable always available in a file, and it contains - at runtime - the path of this file's directory. Since your file in src/ is converted to a file in dist/ and executed there, the actual files being served are in dist/public. Have a look at package.json for its command build (hint: there's something about copying static assets, and another about building Sass).
  4. wrong as per point 3, correct otherwise.

Now you should have the answer to your question. ;)

Sign up to request clarification or add additional context in comments.

1 Comment

I added a file as in the project to copy static assets with the following line: shell.cp("-R", "src/public/css", "dist/public/"); to copy the css files since I was not using scss. js files are created with tsc. Thanks a lot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.