Netpress Laravel-inspired backend framework for Node.js
Frameworkv0.1.14 Starterv0.1.12 Docsv1.0.3
Overview Installation Architecture CLI
Examples

CRUD App Example

This example shows the standard Netpress CRUD pattern:

Route -> Controller -> Service -> Model -> Response

Example Endpoints

  • GET /api/v1/posts
  • POST /api/v1/posts
  • GET /api/v1/posts/:id
  • PUT /api/v1/posts/:id
  • DELETE /api/v1/posts/:id

Service

// app/Services/PostService.js
import Post from '../Models/Post.js';

export default class PostService {
  static all() {
    return Post.latest().get();
  }

  static create(payload) {
    return Post.create(payload);
  }

  static show(id) {
    return Post.findOrFail(id);
  }

  static update(id, payload) {
    return Post.findOrFail(id).then((post) => post.update(payload));
  }

  static async destroy(id) {
    const post = await Post.findOrFail(id);
    await post.delete();
  }
}

Controller

// app/Http/Controllers/PostController.js
import { BaseController } from '@admicaa/netpress';
import PostService from '../../Services/PostService.js';

class PostController extends BaseController {
  async index(_req, res) {
    return res.success(await PostService.all());
  }

  async store(req, res) {
    return res.success(await PostService.create(req.body));
  }

  async show(req, res) {
    return res.success(await PostService.show(req.params.id));
  }

  async update(req, res) {
    return res.success(await PostService.update(req.params.id, req.body));
  }

  async destroy(req, res) {
    await PostService.destroy(req.params.id);
    return res.success(null);
  }
}

export default new PostController();

Why This Pattern Works

  • routes stay small
  • controllers stay thin
  • services hold the app logic
  • responses stay consistent

For a longer end-to-end tutorial, see Quickstart: Blog.