Skip to content

Node.js

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.

Entry file

The entry file for this template must be one of the following: index.js, server.js or app.js. 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:

javascript
const express = require('express');
const app = express();
// Define your routes, then listen on your chosen port
app.listen(process.env.PORT || 3000, () => {
  console.log('Server running');
});

Example with the built-in HTTP server:

javascript
const http = require('http');

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

server.listen(process.env.PORT || 3000, () => {
  console.log('Server running');
});

Version

Running on Node.js v20.0.0

Supported languages

Javascript

Testing framework

Jest

Special reminders and implementation details

js
const { getMessage } = require("./index");

describe("App", () => {
  test("should return 'Hello world!'", () => {
    const message = "Hello World!";
    expect(getMessage(message)).toEqual("Hello World!");
  });
});

Included libraries

How to debug

Debugging JavaScript code is a crucial skill 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:

JavaScript Code

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

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