Socket
Socket
Sign inDemoInstall

mvc-webapp

Package Overview
Dependencies
93
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mvc-webapp

A simple framework for MVC web applications and RESTful APIs.


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
3.97 MB
Created
Weekly downloads
 

Readme

Source

GitHub GitHub release (latest by date) build

mvc-webapp node module

A simple framework for MVC web applications and RESTful APIs.

Features

  • Docker container ready
  • Express based HTTP handling and routes
  • Familiar MVC folder structure and URL paths (controller per file, public folder for static content, etc)
  • Optional shared session management using Redis (falls-back to memorystore)
  • CORS support (HTTP OPTIONS)
  • Flexible logging formatting using Morgan
  • Out of the box support for EJS templates in Views, and partials
  • Use any Node based data access module for storage
  • Custom error handling
  • Tiny and clean; outside of NPM dependencies, the code is about ~200 lines

Setup and First Webapp

  1. Follow these steps to get started with your first mvc-webapp:
mkdir test-app
cd test-app
npm init
npm install mvc-webapp --save
mkdir -p application/models
mkdir -p application/controllers
mkdir -p application/views
mkdir -p application/public

At some point this will be automated by a script, for now, it will involve some keystrokes.

  1. Add an entry point app.js on the root folder. This contains your app options and can be configurable via env-vars for container usage:
#!/usr/bin/env node

'use strict'

const webapp = require('mvc-webapp')

webapp.run({
	applicationRoot: process.env.PWD,
	listenPort: process.env.PORT || '3000',
	sessionRedisUrl: process.env.REDISCLOUD_URL || undefined,
	sessionSecret: process.env.SESSION_SECRET || 'NOT_SO_SECRET',
	redirectSecure: true,
	errorMiddleware: (err, req, res, _) => {
		res.json({
			status: err.status,
			message: err.message,
			stack: req.app.get('env') === 'development' ? err.stack : ''
		})
	}
})

The error handling can be customized to return plain JSON, HTTP codes or an EJS rendered page, your choice.

  1. Add an initial controller, this will be automatically mapped to a path (login.js becomes /login//):
'use strict'

const express = require('express')
const router = new express.Router()

router.get('/', (req, res, _) => {
	res.json({
		status: 'OK',
		data: null
	})
})

module.exports = router

This should be familiar to any Express user. A special exception is made for the index.js controller file, this is mapped to the root / folder. Additionally, any routes inside that controller, get appended as a method.

In order to render the EJS view, invoke the view (file)name in the res.render call:

res.render('index', {
	title: 'Homepage',
	user: 'octopie'
})
  1. Run using npm start or node app.js - added the env var DEBUG="mvc-webapp:*" to see what the framework is doing behind the scenes.

Docker Support

Add the following file to the root folder and docker build:

FROM node:latest

WORKDIR /app
ADD . /app

RUN npm install

CMD ["npm","start"]

Also Checkout

  1. EJS Templates - this is what the views use
  2. Express - this is what powers the HTTP communication

Keywords

FAQs

Last updated on 07 Jul 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc