Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

w3cdpass/replicax

Open more actions menu

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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.