diff --git a/README.md b/README.md index af1fca1..fa05a02 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ A minimal node http server framework by [Mart Anthony Salazar](https://github.com/mart-anthony-stark) - [Getting Started](https://github.com/mart-anthony-stark/Unnamed.js#getting-started) +- [Middlewares](https://github.com/mart-anthony-stark/Unnamed.js#middlewares) - [Routes](https://github.com/mart-anthony-stark/Unnamed.js#routes) - [Request](https://github.com/mart-anthony-stark/Unnamed.js#request-object) - [Response](https://github.com/mart-anthony-stark/Unnamed.js#response-methods) @@ -28,6 +29,20 @@ const server = unnamed({ ![server](https://github.com/mart-anthony-stark/Unnamed.js/blob/main/docs/start%20server.png?raw=true) +### Middlewares + +- Middleware functions are methods that have access to the request object and the response object. + +```javascript +server.middleware(cors("*")); +``` + +````javascript +server.middleware((request, response)=>{ + request.user = user; +}); +``` + ### Routes This framework supports the 5 commonly-used HTTP request methods. The methods can be accessed through the returned server object @@ -54,7 +69,7 @@ server.PATCH("/", (request, response) => { server.DELETE("/", (request, response) => { response.code(200).send({ method: request.method, msg: "Hello world" }); }); -``` +```` ### Request object diff --git a/demoV2/app.js b/demoV2/app.js index a131ffe..92e0cb0 100644 --- a/demoV2/app.js +++ b/demoV2/app.js @@ -1,29 +1,32 @@ -const unnamed = require("../src"); -const mongoose = require("mongoose"); -const cors = require("cors"); +const unnamed = require("unnamed-js"); +// const mongoose = require("mongoose"); +// const cors = require("cors"); // env variables -require("dotenv").config({}); +// require("dotenv").config({}); const server = unnamed({ port: process.env.PORT || 5000, init: async () => { try { - mongoose.connect(process.env.DB_URI, { - useNewUrlParser: true, - useUnifiedTopology: true, - }); - console.log("Connected to database"); + // mongoose.connect(process.env.DB_URI, { + // useNewUrlParser: true, + // useUnifiedTopology: true, + // }); + // console.log("Connected to database"); } catch (error) { console.log(error); } }, }); -server.middleware(cors("*")); +// server.middleware(cors("*")); -server.combineRouters(require("./routes")); +// server.combineRouters(require("./routes")); +const morgan = require("morgan"); +server.middleware(morgan("tiny")); server.GET("/", (req, res) => { res.send("hello"); + console.log("hello"); }); diff --git a/demoV2/package.json b/demoV2/package.json index 7f71714..6eea093 100644 --- a/demoV2/package.json +++ b/demoV2/package.json @@ -4,7 +4,7 @@ "description": "", "main": "app.js", "scripts": { - "dev": "nodemon app.js" + "dev": "nodemon index.js" }, "keywords": [], "author": "", @@ -12,6 +12,8 @@ "dependencies": { "cors": "^2.8.5", "dotenv": "^16.0.0", - "mongoose": "^6.2.1" + "mongoose": "^6.2.1", + "morgan": "^1.10.0", + "unnamed-js": "^1.1.0" } } diff --git a/src/package.json b/src/package.json new file mode 100644 index 0000000..b5f9e05 --- /dev/null +++ b/src/package.json @@ -0,0 +1,19 @@ +{ + "name": "unnamed-js", + "version": "1.0.0", + "description": "", + "main": "index.js", + "directories": { + "lib": "lib" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "node server", + "server", + "http" + ], + "author": "", + "license": "ISC" +}