noda-fw
v1.0.9
Published
This framework was made by kintaro azura chaniago from bencoolen city, indonesia.
Downloads
7
Maintainers
Readme
Noda-fw
This framework was made by kintaro azura chaniago from bencoolen city, indonesia.
Instalation
$ npm install noda-fw
or you can do this
$ npm install github:kintaroazurachaniago/noda-fw
Quick Start
Create one index file. here we create new one index file named "index.js" in the root directory of the project
// getting noda-fw from node_modules
const { Start, Route, Controller } = require('noda-fw')
const { home } = Controller
// register visitable routes
Route.get('/', home.index)
// starting the project
Start(Route, 4120, _ => console.log('Server running on port 4120'))
that's all.
and then try to run this file by execute this command
$ node index
the output in the console should be like this
Server running on port 4120
Database exist! : Using "app" database
Table exist! : Using "users" table
Working on C:\coding\Space\Tutorial\Nodejs\v1
Now you can try to visite http://127.0.0.1:4120 on your browser app.
How to use?
Route
Route object has two method which is get() and post(). both of them need two parameter which is the url as the first one and the callback as the second one. look at here
// Get method
Route.get('/user/create', (req, res) => {
res.end(`<form action="/user/save" method="post"><input type="text" name="username"/><button>Create</button></form>`)
})
// Post method
Route.post('/user/save', (req, res) => {
res.end(JSON.stringify(req.form))
})
Controller
We can store the second parameter of Route.get() and Route.post() method into the other module which is called Controller. in this controller we just need to export the callbacks. and we can also use the local-db as well in the controller file
module.exports = {
// Controller.home
home : {
// Controller.home.index
index : (req, res) => {
res.end('Hello world')
}
},
user : {
create : (req, res) => {
res.end(`<form action="/user/save" method="post"><input type="text" name="username"/><button>Create</button></form>`)
},
save : (req, res) => {
res.end(JSON.stringify(req.form))
}
}
}
and actually we better use res.view() insted of res.end(). res.view() will be reading a file and the execute res.end() with the file content which is has been parsed from noda-template-engine and bring the data for the client as well
module.exports = {
home : {
index : (req, res) => {
/*
the first parameter is the file's path
we need to prepare the file before read it. the file should be written in the process.cwd() and then setting.paths.view folder.
we can change the default views path in the setting.json file by changing the value of setting.paths.view
the second parameter is the data which is will be passed to the noda-template-engine. and then we can use the data in the view file
*/
res.view('home.spa', { title : 'Home page', theme : { bg : 'dark', color : 'light' } })
/*
data.title = 'Home page'
data.theme.bg = 'dark'
data.theme.color = 'light'
*/
}
}
}
Noda-template-engine
How do we use the data from server in the view file? We have two ways to manage and modify the data from the server. they are echo-tag and script-tag
| Echo tag | Script tag | | :--------: | :----------: | | Echo tag is focused for print the data into the client | Script tag is focused for modify the data by the conditioner | | -={ /* data / }=- | -=[ / code */ ]=- |
Example :
<div class="row">
-=[
const fruits = ['apple', 'mango', 'banana']
fuits.forEach( fruit => {
echo(`<div class="col-md-4">Fruit name : ${fruit}</div>`)
})
]=-
</div>
or we can type like this
<div class="row">
-=[ const fruits = ['apple', 'mango', 'banana'] ]=-
-=[ fruits.forEach( fruit => { ]=-
-={ `<div class="col-md-4">Fruit name : ${fruit}</div>` }=-
-=[ }) ]=-
</div>
Model
Noda-fw comes with local-db manager. Local-db file stored in node_modules/noda-fw/DATABASE.ben directory. We can modify the file content using Model object. Model object has some method such as create(), read(), update(), delete() as CRUD. and where() as well as selector Model object will given when we call DB.createDatabase() method. Noda-fw has DB class, so just require the module
const { DB } = require('noda-fw')
Anyway, Both of DB class and Model class should be used in synchronous programming
Example :
const User = async _ => {
return await DB.createDatabase('user')
}
Then we can call Model modthods
- User.create(data)
- User.read(field)
- User.update(newData)
- User.delete()
Just becareful when we use update and delete, it will affect to all of selected table data. unless we define the selected data using where()
- User.where('id', 1).update({ age : 14 })
- User.where('age', '>', '18').delete()
I think it's enough for creating a small scale project in nodejs.
If you have a question, Please contact me on :