Simplemvc.js – A highly opinionated web back end framework

栏目: IT技术 · 发布时间: 4年前

内容简介:Approximately a week ago, I started on the journey to rebuild one of my side projects in Node.js. It was originally built in asp.net core mvc. There was nothing wrong with how it was built, it wasn't misbehaving or anything like that, but I needed to make

Approximately a week ago, I started on the journey to rebuild one of my side projects in Node.js. It was originally built in asp.net core mvc. There was nothing wrong with how it was built, it wasn't misbehaving or anything like that, but I needed to make some changes, add a feature that has been requested dozens of times now, and the code base was stale and packages needed updating, etc.

So I opened the project in Visual Studio, and started re-familiarizing myself with the codebase. After an hour it was painfully clear, I wasn't going to add any more code to this project, at least not as it currently existed. So I decided to just do a Node/Express/MongoDB application since it was already using MongoDB for it's backend.

A Few Hours Later

I was exhausted. I tried to do everything the right way, the way my code-forefathers would have expected, but the code was starting to pile up, and I hadn't even rendered the homepage yet. It made me miss the days of just creating an index.php page and shit just rendering. The amount of boilerplate code I was writing was getting very annoying, so I decided that THIS is the project I was going to tackle first.

I contemplated code generation for the boilerplate, but that is fraught with edge cases and constantly needing to be tweaked. Then I thought, why not just abstract it all away in a tiny package, and get it out of my way so I can just build my routes and my views, but also make it feel more like the asp.net mvc world I come from.

A new web framework is born

I decided to follow a little bit of the MVC pattern, or at least the pattern than asp.net mvc tutorials have always followed, which is more like Controllers, Views, View-Models, but Microsoft can call that MVC, I will call my web framework MVC as well.

The basic concept is the library sets up everything you could need (so long as what you need is MongoDB, sessions, and mustache for views), and then it gets out of your way for you to create your Controllers (which is really just a route handler with some nice helper methods) anyway you wish (all in one file, highly structured, it doesn't matter to SimpleMVC.js).

The first project I decided to build with the framework was a little example blog. Total code is around 45 lines (excluding the JSON data source), and for about 20 more lines, you could have blog backed by a MongoDB collection, or even straight from the file system. In fact, this blog is built using SimpleMVC.js, and the code including admin and supporting comments (though I have them turned off) is under 200 lines.

You can see the sample blog in the repository , and you can use that to get started. I'm still writing the full documentation, but it is a pretty simple framework to get started with. You can install it from npm today , but it is still a beta .

Why use it?

SimpleMVC.js was made to get you setup quickly to start building your next web application in a structured way without all the excess boilerplate that typically comes with an MVC project in other frameworks.

The framework has 4 built in classes.

  1. SimpleMVC.App - the main service, this is what runs your application
  2. SimpleMVC.Controller - this is a class you will new up for every set of routes you want to use
  3. SimpleMVC.Membership - this service has a handful of helper methods for CRUDing users and their profiles
  4. SimpleMVC.SMTP - this service is basically just a wrapper for a preconfigured nodemailer transport

Hello World

As is typical with all new libraries, here is Hello World:

const SimpleMVC = require('simplemvcjs');

const homeController = new SimpleMVC.Controller("/", {
    "": function() {
        return this.content("hello world");
    }
});

const app = new SimpleMVC.App();
app.initDbConnection();
app.addControllers(homeController);
app.listen();

The code can get more complicated from here, but it doesn't have to. A simple dynamic website that doesn't need much user interaction, can really be only a few routes and a database lookup.

Where it goes from here

I have a handful of additional helper methods I want to add to the Controller class, along with supporting all of the HTTP verbs (it currently only supports POST and GET), and then I'd like to finish the Example SaaS project which will use all of the features of the framework, and can be used as a testbed for further development.


以上所述就是小编给大家介绍的《Simplemvc.js – A highly opinionated web back end framework》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

新经济,新规则

新经济,新规则

[美]Kevin Kelly / 刘仲涛 / 译言·东西文库/电子工业出版社 / 2014-7 / CNY 45.00

近年来,互联网持续震动着全世界各个行业以至于整个经济规则……在中国,以小米为代表的各类“互联网思维”轰轰烈烈地颠覆着各个行业……而这一切的一切,凯文凯利早就通过这本小书轻松写定。《新规则,新经济》一书介绍互联网时代,互联网影响下的经济运行的十个新游戏规则。一起来看看 《新经济,新规则》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具