Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
119c821
add mocha.opts,update package.json,add project_directory_structure.md…
Defirence Apr 7, 2025
73c52e5
update the file extension of project_directory_structure.md to .txt
Defirence Apr 7, 2025
3e670fc
fix workflow
Defirence Apr 10, 2025
14a51a9
add branch rule for tests branch
Defirence Apr 10, 2025
ad21571
disable SSH-key auth in favour of HTTPS clone
Defirence Apr 10, 2025
07a77f6
optimize the workflow file
Defirence Apr 10, 2025
25f0934
add exisiting yarn.lock to root path of repo
Defirence Apr 10, 2025
0599e10
migrate from .json eslint config to new spec, update webserver logic …
Defirence Apr 10, 2025
b20be9d
Merge branch 'tests' of https://github.com/Defirence/javascript into …
Defirence Apr 10, 2025
4220015
add eslint.config.mjs to root of monorepo structure.
Defirence Apr 10, 2025
47c64e3
update node-version variable to use 22 instead of 16(?)
Defirence Apr 10, 2025
db8f591
update project workflow structure dependencies for monorepo structure
Defirence Apr 10, 2025
7c72ea5
restructure entire project for easier management.
Defirence Apr 10, 2025
ebeb6e2
update path for tests and linting
Defirence Apr 10, 2025
65f8c7b
update the workflow to verify the lock file exists
Defirence Apr 10, 2025
b8bcc55
commit and push yarn.lock to root of repo.
Defirence Apr 10, 2025
57e3eb1
update workflow spec again
Defirence Apr 10, 2025
75c78a0
update workflow path
Defirence Apr 10, 2025
eec47ab
update workflow
Defirence Apr 10, 2025
e319b67
remove yarn lockfile verification step
Defirence Apr 10, 2025
89894f3
update working-dir path
Defirence Apr 10, 2025
cd2b9d1
update linting workflow, add badge for Mocha tests
Defirence Apr 10, 2025
f473297
update README with badges and update linting workflow
Defirence Apr 10, 2025
aec2361
update root readme.md before merge to main
Defirence Apr 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions .github/workflows/eslint.yml

This file was deleted.

31 changes: 31 additions & 0 deletions .github/workflows/lint-with-jshint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Lint with JSHint

on:
push:
branches:
- main
- dev
- tests
pull_request:

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
cache: 'yarn'

- name: Install dependencies
working-directory: node-express-server
run: yarn install

- name: Run JSHint lint
working-directory: node-express-server
run: npx jshint webserver.js
31 changes: 31 additions & 0 deletions .github/workflows/run-mocha-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Run Mocha Tests

on:
push:
branches:
- main
- dev
- tests
pull_request:

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
cache: 'yarn'

- name: Install dependencies
working-directory: node-express-server
run: yarn install

- name: Run Mocha tests
working-directory: node-express-server
run: npx mocha test/webserver.test.js
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# javascript
[![eslint](https://github.com/Defirence/javascript/actions/workflows/eslint.yml/badge.svg?branch=main)](https://github.com/Defirence/javascript/actions/workflows/eslint.yml)

A collection of JavaScript boilerplate templates, some basic configurations and hello-world projects as a portfolio.
Random JavaScript stuff for fun. :)

[![Mocha Tests](https://github.com/Defirence/javascript/actions/workflows/run-mocha-tests.yml/badge.svg)](https://github.com/Defirence/javascript/actions/workflows/run-mocha-tests.yml) [![Lint JSHint](https://github.com/Defirence/javascript/actions/workflows/lint-with-jshint.yml/badge.svg)](https://github.com/Defirence/javascript/actions/workflows/lint-with-jshint.yml)

<p xmlns:dct="http://purl.org/dc/terms/" xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#">
<a rel="license"
Expand Down
17 changes: 0 additions & 17 deletions boiler-plate/node-express-server/.eslintrc.json

This file was deleted.

9 changes: 0 additions & 9 deletions boiler-plate/node-express-server/README.md

This file was deleted.

41 changes: 0 additions & 41 deletions boiler-plate/node-express-server/webserver.js

This file was deleted.

13 changes: 13 additions & 0 deletions node-express-server/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"esversion": 6,
"node": true,
"browser": true,
"globals": {
"describe": true,
"it": true,
"before": true,
"after": true
},
"undef": true,
"unused": true
}
4 changes: 4 additions & 0 deletions node-express-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# node-express-server
A simple node.js webserver with basic API functionality, written in pure JavaScript.

[![Mocha Tests](https://github.com/Defirence/javascript/actions/workflows/run-mocha-tests.yml/badge.svg)](https://github.com/Defirence/javascript/actions/workflows/run-mocha-tests.yml) [![Lint JSHint](https://github.com/Defirence/javascript/actions/workflows/lint-with-jshint.yml/badge.svg)](https://github.com/Defirence/javascript/actions/workflows/lint-with-jshint.yml)
3 changes: 3 additions & 0 deletions node-express-server/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--reporter spec
--timeout 5000
test/**/*.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
"author": "Defirence",
"license": "CC0-Universal",
"dependencies": {
"eslint": ">=5.16.0",
"express": "^5.1.0",
"supertest": "^7.1.0",
"user-agents": "^1.1.502"
},
"devDependencies": {
"eslint-config-google": "^0.14.0"
"eslint-config-google": "^0.14.0",
"jshint": "^2.13.6",
"mocha": "^11.1.0",
"sinon": "^20.0.0"
}
}
76 changes: 76 additions & 0 deletions node-express-server/test/webserver.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const request = require('supertest');
const sinon = require('sinon');
const { app } = require('../webserver');

describe('Webserver API Tests', () => {
it('should respond with 200 and JSON content type for GET /', (done) => {
request(app)
.get('/')
.expect('Content-Type', /json/)
.expect(200, done);
});

it('should respond with 201 for successful resource creation on POST /user', (done) => {
request(app)
.post('/user')
.send({ name: 'John Doe', age: 30 }) // Simulate valid data
.expect(201, done);
});

it('should respond with 204 for successful resource deletion on DELETE /user', (done) => {
request(app)
.delete('/user')
.expect(204, done);
});

it('should respond with 404 for a missing route', (done) => {
request(app)
.get('/nonexistent-route')
.expect(404, done);
});

it('should respond with 400 for a bad request on POST /', (done) => {
request(app)
.post('/')
.send({ invalid: 'data' }) // Simulate a bad request
.expect(400, done);
});

it('should respond with 500 for server errors on PUT /user', (done) => {
request(app)
.put('/user')
.send({ simulateError: true }) // Trigger the simulated error
.expect(500, done);
});

// Test for PUT /user without simulateError
it('should respond with 200 for successful PUT /user without simulateError', (done) => {
request(app)
.put('/user')
.send({}) // No simulateError in the body
.expect(200, { message: 'Resource updated successfully' }, done);
});

// Test for unsupported Content-Type
it('should respond with 415 for unsupported Content-Type', (done) => {
request(app)
.post('/user')
.set('Content-Type', 'text/plain') // Unsupported Content-Type
.send('name=John&age=30') // Plain text payload
.expect(415, done);
});

// Test for DELETE /user with query params
it('should respond with 204 for DELETE /user even with query params', (done) => {
request(app)
.delete('/user?force=true') // Add query params
.expect(204, done);
});

// Test for undefined HTTP methods
it('should respond with 404 for undefined HTTP methods', (done) => {
request(app)
.patch('/user') // PATCH is not defined for /user
.expect(404, done);
});
});
70 changes: 70 additions & 0 deletions node-express-server/webserver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const express = require('express');
const app = express();
const port = 3000;
const UserAgents = require('user-agents');
const userAgent = new UserAgents().toString();
console.log(userAgent); // Add this to verify the value
const dir = '/user';

app.use(express.json()); // Middleware to parse JSON request bodies

// Middleware to handle unsupported Content-Type
app.use((req, res, next) => {
const supportedTypes = ['application/json'];
if (req.method === 'POST' && !supportedTypes.includes(req.headers['content-type'])) {
return res.status(415).send({ error: 'Unsupported Media Type' });
}
next();
});

app.get('/', (req, res) => {
res.status(200).json({ message: 'Hello Express!' }); // Respond with JSON
console.log(`Received GET request to the API with userAgent:`);
console.log(userAgent.toString());
});

app.post('/', (req, res) => {
console.log(`User-Agent: ${req.headers['user-agent']}`); // Log the User-Agent header
res.status(400).send({ error: 'Bad Request' });
});

app.put('/user', (req, res) => {
try {
// Ensure the request body is parsed and simulate an error if requested
if (req.body && req.body.simulateError) {
throw new Error('Simulated server error');
}
res.status(200).json({ message: 'Resource updated successfully' });
console.log(`Received PUT request from port: ${port} to ${dir}userAgent:`);
console.log(userAgent.toString());
} catch (error) {
res.status(500).json({ error: 'Internal server error' }); // Handle server error
console.error('Error occurred on PUT /user:', error.message);
}
});

app.delete('/user', (req, res) => {
res.status(204).send(); // Respond with 204 for successful deletion
console.log(`Received DELETE request from port: ${port} to ${dir}userAgent:`);
console.log(userAgent.toString());
});

// POST /user route
app.post('/user', (req, res) => {
const { name, age } = req.body;
if (name && age) {
// Simulate resource creation
res.status(201).send({ message: 'User created successfully' });
} else {
res.status(400).send({ error: 'Invalid user data' });
}
});

if (require.main === module) {
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
}

// Export the app and userAgent for testing
module.exports = { app, userAgent };
Loading