Skip to content

Commit 6103dbb

Browse files
committed
feat: update package.json and add Express API
- Updated package description and main entry point in package.json - Added start, build, test, and lint scripts - Introduced express as a dependency - Added eslint, jest, and supertest as devDependencies - Created src/app.js for the Express API with routes for health check and text processing - Added src/server.js to start the Express server
1 parent b54e1e2 commit 6103dbb

8 files changed

Lines changed: 5680 additions & 7 deletions

File tree

node-example/__tests__/app.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const request = require('supertest');
2+
const app = require('../src/app');
3+
4+
describe('API endpoints', () => {
5+
test('GET / returns welcome message', async () => {
6+
const res = await request(app).get('/');
7+
expect(res.status).toBe(200);
8+
expect(res.body.message).toContain('Node.js Example API');
9+
});
10+
11+
test('GET /health returns healthy status', async () => {
12+
const res = await request(app).get('/health');
13+
expect(res.status).toBe(200);
14+
expect(res.body.status).toBe('healthy');
15+
});
16+
17+
test('POST /removespace removes whitespace from text', async () => {
18+
const res = await request(app)
19+
.post('/removespace')
20+
.send({ text: 'hello world' });
21+
expect(res.status).toBe(200);
22+
expect(res.body.result).toBe('helloworld');
23+
});
24+
25+
test('POST /removespace returns 400 when text is missing', async () => {
26+
const res = await request(app)
27+
.post('/removespace')
28+
.send({});
29+
expect(res.status).toBe(400);
30+
expect(res.body.error).toContain('Missing');
31+
});
32+
33+
test('POST /removespace returns 400 for non-string input', async () => {
34+
const res = await request(app)
35+
.post('/removespace')
36+
.send({ text: 123 });
37+
expect(res.status).toBe(400);
38+
});
39+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const removespace = require('../index');
2+
3+
describe('removespace utility', () => {
4+
test('removes spaces from a string', () => {
5+
expect(removespace('a b c')).toBe('abc');
6+
});
7+
8+
test('removes tabs and newlines', () => {
9+
expect(removespace('a\tb\nc')).toBe('abc');
10+
});
11+
12+
test('returns empty string when given only whitespace', () => {
13+
expect(removespace(' ')).toBe('');
14+
});
15+
16+
test('returns same string when no whitespace present', () => {
17+
expect(removespace('abc')).toBe('abc');
18+
});
19+
20+
test('throws TypeError for non-string input', () => {
21+
expect(() => removespace(123)).toThrow(TypeError);
22+
});
23+
});

node-example/eslint.config.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { defineConfig } = require("eslint/config");
2+
const js = require("@eslint/js");
3+
4+
module.exports = defineConfig([
5+
js.configs.recommended,
6+
{
7+
languageOptions: {
8+
globals: {
9+
require: "readonly",
10+
module: "readonly",
11+
exports: "readonly",
12+
__dirname: "readonly",
13+
__filename: "readonly",
14+
process: "readonly",
15+
console: "readonly",
16+
},
17+
},
18+
rules: {
19+
"no-unused-vars": "warn",
20+
},
21+
},
22+
{
23+
files: ["__tests__/**"],
24+
languageOptions: {
25+
globals: {
26+
describe: "readonly",
27+
test: "readonly",
28+
expect: "readonly",
29+
beforeAll: "readonly",
30+
afterAll: "readonly",
31+
beforeEach: "readonly",
32+
afterEach: "readonly",
33+
},
34+
},
35+
},
36+
]);

node-example/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = function removespace(string) {
2-
if (typeof string !== "string") throw new TypeError("Meh! Enter a string!!");
3-
return string.replace(/\s/g, "");
2+
if (typeof string !== "string") throw new TypeError("Meh! Enter a string!!");
3+
return string.replace(/\s/g, "");
44
};

0 commit comments

Comments
 (0)