{"id":23222979,"url":"https://github.com/dckt/shortlink","last_synced_at":"2025-04-05T16:29:39.121Z","repository":{"id":29862348,"uuid":"33407407","full_name":"DCKT/ShortLink","owner":"DCKT","description":"Alias and shortened URL service","archived":false,"fork":false,"pushed_at":"2015-04-04T21:37:32.000Z","size":164,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-11T13:27:30.984Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DCKT.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-04T14:03:32.000Z","updated_at":"2015-04-04T21:37:32.000Z","dependencies_parsed_at":"2022-09-07T00:40:30.554Z","dependency_job_id":null,"html_url":"https://github.com/DCKT/ShortLink","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DCKT%2FShortLink","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DCKT%2FShortLink/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DCKT%2FShortLink/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DCKT%2FShortLink/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DCKT","download_url":"https://codeload.github.com/DCKT/ShortLink/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247365025,"owners_count":20927246,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-18T23:15:37.422Z","updated_at":"2025-04-05T16:29:39.098Z","avatar_url":"https://github.com/DCKT.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Express boilerplate\n================\n\nI like Ruby On Rails and his structure so I decided to follow their MVC conventions with the node module expressjs. I'm also inspired\nwith the Ember.js framework that I like too.\n\n\n## Install\n\nAll the code is written with ES6 syntax, so you have to install [Babel](https://babeljs.io/docs/using-babel/) and use the `babel-node` command.\n\n\n- Clone the repo \n- `npm i`\n- `npm start`\n\n## Structure\n\nMVC pattern is cool and simple to undertand, so it's based on him.\nFirst, you have an **app** folder who will contain all code of your application :\n\n**app/**\n- [controllers](#controller)\n- [models](#models)\n- [views](#views)\n- [routes](#routes)\n- \u003ca href=\"#routerjs\"\u003eRouter.js\u003c/a\u003e\n \n**assets/**\n\n**config/**\n### Controller\n\nA controller is design to handle the logic of your application, so he will handle the request and send the response. You should create **one controller per route**, so if you create a **/users** route the controller should be named **UsersController**. This pattern allow you to quickly find the file you want in your text editor. \n\nThe structure of a controller is simple, export an object of methods. Here is an example :\n```js\n// IndexController.js\n\nexport default {\n  index: {\n    get(req, res) {\n      res.locals.title = \"Home\";\n      res.render('index');\n    }\n  }\n};\n```\n\n### Models\n\nThis boilerplate use MySQL as default. The configuration file is called **mysql.js** and is located in the **config** folder. Here is the basic setup :\n```js\nimport mysql from 'mysql';\n\nvar connection = mysql.createConnection({\n  host: 'localhost',\n  database: 'test',\n  user: 'root',\n  password: ''\n});\n\nconnection.connect(function (err) {\n  if (err) {\n    console.error('error connecting: ' + err.stack);\n    return;\n  }\n});\n\nexport default connection;\n```\n\nDon't forget to use the correct database and login/password ! You can now create a model based on the route or something you want like **Book.js**. This file will use a function utility called **query**. Here is an example :\n\n```js\n// Book.js\nimport query from '../../utils/query';\n\nexport default {\n  findAll(cb) {\n    return query('SELECT * FROM books');\n  },\n  findById(id) {\n    return query('SELECT * FROM books WHERE id = ?', [id]);\n  }\n}\n```\n\nquery is designed for simplifying the model and **use Promise** ! So when you call the method in your controller, you have to use the **then** and **catch** functions :\n```js\n// BooksController.js\nimport Book from '../models/Book';\n\nexport default {\n  index: {\n    get(req, res) {\n    \n      Book.findAll()\n        .then(books =\u003e {\n          res.locals.title = \"Home\";\n          res.locals.books = books;\n          res.render('index');\n        })\n        .catch(err =\u003e {\n          console.error(err);\n        });\n\n    }\n  }\n}\n```\n\nEasy to read and understand isn't it ?\n\n### Views\n\nThis boilerplate use **Jade** for templating, if you prefer ejs, you just need to edit the `middleware.js` file in the config folder.\n\nAs I said for controller and routes, it's almost the same for the views. You should create a folder based on your routes (ex: users), create a\n**users** folder who will contain all the view concerned by the user.\n\n### Routes\n\nThe routes files are used for making the relation between your controller and the URLs. Here is an example :\n```js\n// IndexRoute.js\n\n/**\n* Home Route\n* path: /\n******************** */\n\nimport express from \"express\";\nimport Controller from \"../controllers/IndexController\";\n\nvar router = express.Router();\n\n\nrouter.get('/', Controller.index.get);\n\nexport default router;\n```\n\nAs you can see, I put a function according the HTTP verb like here **index.get** for the GET on `/`.\n\n### Router.js\n\nThe **Router.js** may intrigue you, it serves to handle all of your routes. Here is an example :\n```js\n// Router.js\n\nexport default {\n  Index: require('./routes/IndexRoute')\n}\n```\nNow you have to import just one file in your **server.js** :\n```js\n// server.js\n\nimport { Index } from './app/Router';\n```\n\n## Assets\n\nThis boilerplate set the **assets** folder for all of your static files. Some folders are already available :\n\n- stylesheets\n- javascripts\n- images\n- fonts\n\nIn your views, the links will look like this : `/stylesheets/style.css` or `/images/logo.png`.\n\n## Config\n\nThe folder **config** is design to keep all config files like middleware, mysql config, etc..\nThe idea of this folder is to separate the logic of the configuration. With MySQL as example, you just have to create the connection and export the object for making queries.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdckt%2Fshortlink","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdckt%2Fshortlink","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdckt%2Fshortlink/lists"}