GitHunt
W3

w3cdpass/replicax

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

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

email : kupasva663@gmail.com