1.0.24 • Published 9 months ago

http-node-routing v1.0.24

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

http-node-routing

This project was motivated by the will of understanding how things work. Im mainly using node http module to provide a router to handle HTTPS requests and responses. On future versions i will use a generic entity that can peform these operations on a POSTGRES database. It should be used mainly for educational propurses. Currently has support for postgresql

Technologies used

My Skills

Steps

1 - Installation

install

npm install http-node-routing

install dotenv

npm install dotenv

inside the file that you instance the server

import dotenv from 'dotenv';
dotenv.config();

1 - Config your env variables

create an .env file with the port variable on the root of the project

2 - Simple usage example

check folder examples for the basic and the examples that persists data into the database

src/examples
import router from 'http-node-routing'

const port = process.env.PORT || 3000;

const app = router()

app.listen(port, () => console.log(`listening on ${port}`))
app.get('/', (req, res) => {
	res.send('Hello World')
})
app.post('/users', (req, res) => {
	const user = req.body
	res.send(user)
})

Usage with postgres

1 - Start your postgres server

2 - Add the port that the server is running into the database key attribute from connection object

2 - Pass the table that you will perform the CRUD operations on the routes address

Disclaimer: I have added only SELECT and INSERT operations for now

import dotenv from 'dotenv';

import { databaseFunctions } from 'src/connection';

import { createNodeRouter } from '../../nodeRouter';
dotenv.config();

const port = process.env.PORT || 3000;

const app = createNodeRouter();

const connection = {
	user: process.env.PG_USER,
	host: process.env.HOST,
	password: process.env.PASSWORD,
	database: process.env.DATABASE,
	port: process.env.DB_PORT,
};

const { connectDb, getAllRegistersFromTable, insertIntoTable } =
	await databaseFunctions(connection);

app.listen(port, () => console.log(`listening on ${port}`));

connectDb();

app.get('/users', async (req, res) => {
	const registers = await getAllRegistersFromTable('users');
	res.send(registers);
});


app.post('/users', (req, res) => {
	const { email, password } = req.body!;
	const columns = ['email', 'password'];
	const valuesToInsert = [email, password];
	insertIntoTable('users', columns, valuesToInsert, res);
});

Clone the repo

git clone https://github.com/rpajf/http-node-routing

run for the basic example:

npm run start:basic

run for the persisting into postgres example:

npm run start:db

Support for other databases rather than postgres

Im implementing support for sqlite3 and mysql

How to contribute?

1 - Implement other methods for the CRUD operations on the Postgres databse

2 - Add more tests to the library routing

still in progress...

next steps:

  • Handle the req.params and req.query
  • Creating the connection with databases (postgres and mysql)
1.0.19

9 months ago

1.0.18

9 months ago

1.0.22

9 months ago

1.0.21

9 months ago

1.0.20

9 months ago

1.0.24

9 months ago

1.0.23

9 months ago

1.0.17

9 months ago

1.0.16

9 months ago

1.0.15

9 months ago

1.0.14

9 months ago

1.0.12

9 months ago

1.0.11

9 months ago

1.0.10

9 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago