内容简介: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.
- SimpleMVC.App - the main service, this is what runs your application
- SimpleMVC.Controller - this is a class you will new up for every set of routes you want to use
- SimpleMVC.Membership - this service has a handful of helper methods for CRUDing users and their profiles
- 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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。