Skip to content

w3cdpass/replicax

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Replicax - A lightweight, zero dependices, middleware, auto-reload support, HTTP server framework

Node.js Version Dependencies License

Installation

npm i replicax --save

πŸ“š Table of Contents

  1. πŸš€ Usage
  2. πŸ”„ Auto Reload
  3. πŸ“‘ HTTP Methods
  4. πŸ”‘ Middleware
  5. πŸ”’ Ready for Production (HTTPS)
  6. 🐞 Issues
  7. 🀝 Author

1 πŸš€ Usage

const { replicax } = require('replicax');
const app = replicax();

// Simple GET route
app.get('/', (req, res) => {
  res.json({ message: 'Hello from Replicax!' });
});

// Route with parameters
app.get('/users/:id', (req, res) => {
  res.json({ userId: req.params.id });
});

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

2 πŸ”„ Auto Reload

// package.json
"scripts": {
    "start": "replicax index.js"
  },

/lib/watch.js - File watcher that automatically restarts your server when index.js files change of your main project, with debouncing to avoid rapid restarts, ignores node_modules


3 πŸ“‘ HTTP Methods

GET

app.get('/users', (req, res) => {
  res.json({ users: getAllUsers() });
});

POST

app.post('/users', async (req, res) => {
  const newUser = createUser(req.body);
  res.status(201).json(newUser);
});

PUT

app.put('/users/:id', (req, res) => {
  updateUser(req.params.id, req.body);
  res.json({ success: true });
});

PATCH

app.patch('/users/:id', (req, res) => {
  partiallyUpdateUser(req.params.id, req.body);
  res.json({ updated: true });
});

DELETE

app.delete('/users/:id', (req, res) => {
  deleteUser(req.params.id);
  res.status(204).end();
});

Route Chaining

app.route('/articles/:id')
  .get((req, res) => res.json(getArticle(req.params.id)))
  .put((req, res) => res.json(updateArticle(req.params.id, req.body)))
  .delete((req, res) => res.status(204).end());

4 πŸ”‘ Middleware

// Authentication middleware
app.use('/admin', (req, res, next) => {
  if (req.headers.authorization === 'secret') {
    next();
  } else {
    res.status(401).json({ error: 'Unauthorized' });
  }
});

custom

// define middleware
const authMiddleware = (req, res, next) => {
  if (!req.headers.authorization) {
    return res.status(401).json({ error: 'Unauthorized' });
  }
  next();
};

// use any where or a particular route
app.get('/protected', authMiddleware, (req, res) => {
  res.json({ data: 'Secret data' });
});

5 πŸ”’ Ready for Production (HTTPS)

const fs = require('fs');
const { replicax } = require('replicax');

const app = replicax();

const options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};

app.listen(443, () => {
  console.log('HTTPS server running');
}, { https: true, httpsOptions: options });

🀝 Author

GitHub: @w3cdpass

About

Replicax - A lightweight, zero dependices, middleware, auto-reload support, HTTP server framework.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published