nucklejs
Version:

107 lines (86 loc) • 3.97 kB
Markdown

NuckleJS is a node.js framework built on top of express.js. It's build for creating web applications just like you whould with express but with less code, files and effort. NuckleJS is fast, you can get a server up and running in just two lines of code
## Installation
Use the package manager [npm](https://npmjs.com) to install NuckleJS.
```npm
npm install nucklejs
```
## Start a server
```javascript
const { createServer } = require('nucklejs')
//The createApp method will also return the express app itself
const server = createServer(8080)
```
### And that's it!
You now have a server running on port 8080.
You may have noticed that it says "Production Mode" in the console. That's becouse NuckleJS comes pre-configured for .env files and is looking at the NODE_ENV variable
## Configure .env
Here you'll see how to configure your .env file. First of all create a new file named ".env". Now, down below you'll see what variable to define to tell NuckleJS what environment you're working in.
You can also define your server port here by simply defining a PORT variable
```.env
NODE_ENV=development
PORT=8080
````
## Routing
Routing in NuckleJS is a bit diffrent from express. BUT it's simple!
The method createRouter() is used to define your router containing all of your routes. Let me show you
```javascript
const { createRouter } = require('nucklejs')
const mainController = require('./controllers/main.js')
const router = createRouter([
{
path: '/',
method: 'GET',
controller: mainController.root
}
])
module.exports = router
```
That's how easy it is to define your routes. I recommend you creating your router in router.js file for a good project structure. But now, how do we use our routes in our application...Simple, in the createServer() method you can pass in your router as the second argument
```javascript
const server = createServer(8080, require('./router.js')
```
#### Simple as that!
Now NuckleJS will handle the rest your routes are now in use in your application
## Route Guards
Now to route guards. What is it? It's a middleware you run before the actuall request controller / handler. Like a middle man. It's often used when handeling access to certain endpoins. Let me show you...
```javascript
const { createRouter } = require('nucklejs')
const mainController = require('./controllers/main.js')
const { mainGuard } = require('./guards/main.js'=
const router = createRouter([
{
path: '/',
method: 'GET',
controller: mainController.root
},
{
path: '/protected',
method: 'POST',
controller: maincontroller.protected,
guard: mainGuard
}
])
module.exports = router
```
It's that simple to create a protected route using NuckleJS. Now our middleware "mainGuard" will run before the actuall controller for the route does
## Using other middlewares
You may want to use something like cors in your application. No problem, like i said the createServer() also return the actuall app itself. So you can simply do:
````javascript
const cors = require('cors')
const server = createServer(8080, require('./router.js')
server.use(cors())
````
It's that simple to use any other middleware you like
## Cookies
Sometimes you may want to use cookies in your application eighter set or read cookies. NuckleJS comes pre-loaded with cookie-parser and is therefore ready to set and read cookies. Example:
````javascript
//This is an example of an controller for a route
module.exports.main = (req,res) => {
res.cookie('My-Name', 'Rasmus Nitsche - NitscheDev')
res.send('Yay, a cookies has been set')
}
````
And it's that easy. If you want to know more about cookies check out cookie-parser's documentation...
## License
[MIT](https://choosealicense.com/licenses/mit/)