Skip to content

Node.js (TypeScript) ​

You should define a main module as the entry point of your Node.js application. Your Node.js code should be linted using ESLint with the recommended configuration for Node.js and TypeScript.

Entry file ​

The entry file for this template must be one of the following: index.ts, server.ts, or app.ts. The first one present is used as the entry point.

Server configuration for API Tester ​

For API challenges, ensure your server is started from the entry file so it is running when you use the API Tester tab. The API Tester is available for Node.js templates to send requests and test your endpoints.

Example with Express:

typescript
import express from "express";
const app = express();
// Define your routes, then listen on your chosen port
app.listen(3000, () => {
  console.log("Server running");
});

Example with the built-in HTTP server:

typescript
import http from "http";

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "application/json" });
  res.end(JSON.stringify({ message: "Hello World!" }));
});

server.listen(3000, () => {
  console.log("Server running");
});

Version ​

Running on Node.js v20.0.0

Supported languages ​

TypeScript

Testing framework ​

Jest

Special reminders and implementation details ​

ts
import { describe, expect, it } from "@jest/globals";

import { helloWorld } from "./index";

describe("App", () => {
  it("should return 'Hello, world!'", () => {
    expect(helloWorld()).toEqual("Hello, world!");
  });
});

Included libraries ​

Write-tests challenges ​

This template supports write-tests challenges. The reference solution is already written and you write the tests: they must pass on the reference solution and catch every hidden bug. Run Tests runs your tests against the reference solution only, while Verify tests and Submit also run them against hidden copies of it that each contain one bug. Read Solving a write-tests challenge for the full flow.

  • Test runner: Jest (ts-jest)
  • describe, it and expect are available as globals. You can also import them from @jest/globals for explicit typing.
  • Name test files *.test.ts. A type error fails the whole test file, so keep your tests type-correct.

A complete example is in Write-tests examples by template.

Reading input (stdin) ​

Programs that read values use standard input. Run opens an interactive terminal: the program starts, prints its prompt, and you type the answer and press Enter, as in a real terminal. The tests of an output-test challenge feed the input themselves.

ts
import { createInterface } from 'readline';

const rl = createInterface({ input: process.stdin });

rl.once('line', (name: string) => {
  console.log(`Hello, ${name.trim()}!`);
  rl.close();
});

readline hands over each line as it arrives, so it works both in the terminal and with the input the tests pipe in. Use rl.on('line', ...) to read several values. Do not print a prompt in output-test challenges, rl.question('Your name: ', ...) included: it writes the prompt to standard output, where it becomes part of the compared output. Avoid fs.readFileSync(0): it reads until end-of-file, which never comes while someone is typing in a terminal.

How to debug ​

Debugging TypeScript Node.js code is crucial for any developer. One of the simplest yet effective ways to debug is by using console.log() statements.

Steps to Debug Using Console Logs ​

  1. Identify the Problem Area Locate the section of your code where you suspect the problem might be.

  2. Insert console.log() Statements Add console.log() statements before and after the suspected problematic code to print out variable values and flow control.

  3. Check the Results Output of your tests

  4. Analyze the Output Look at the values printed in the console to understand what's going wrong in your code. Adjust your code based on the findings and repeat as necessary.

Here's an example of how to use console.log() for debugging in TypeScript:

typescript
function calculateSum(a: number, b: number): number {
  console.log("calculateSum called with arguments:", a, b); // Log function call
  const sum: number = a + b;
  console.log("Sum after addition:", sum); // Log sum calculation
  return sum;
}

const result: number = calculateSum(5, 10);
console.log("Result of calculateSum:", result); // Log the result