{"id":17274191,"url":"https://github.com/davidthorn/express-server","last_synced_at":"2025-03-26T13:26:42.445Z","repository":{"id":122657366,"uuid":"160246562","full_name":"davidthorn/express-server","owner":"davidthorn","description":"A test project to learn about how the node express server works","archived":false,"fork":false,"pushed_at":"2018-12-03T21:34:14.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T14:47:51.785Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/davidthorn.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-03T20:01:20.000Z","updated_at":"2018-12-03T21:34:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"e3bc13e7-9295-4b64-88e1-5b8d1e07d789","html_url":"https://github.com/davidthorn/express-server","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/davidthorn%2Fexpress-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2Fexpress-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2Fexpress-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2Fexpress-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidthorn","download_url":"https://codeload.github.com/davidthorn/express-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245661507,"owners_count":20651877,"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-10-15T08:53:20.540Z","updated_at":"2025-03-26T13:26:42.416Z","avatar_url":"https://github.com/davidthorn.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express Server - Examples\n\nThis is a test project which allows for me to learn and document all my finding about how you create express servers.\n\n### Installation\n\n\u003e Install the express package \n\n```bash\nnpm install --save-dev express\n```\n\n### Create a basic server\n\nFrom what it seems all we have to do is to instantiate an express object.\n\n```javascript\nconst express = require('express');\nconst app = express();\n```\n\nAdd a listener to a route.\n```javascript\napp.get('/home' , (request, response) =\u003e {\n    response.send('Hello world');\n})\n```\n\nThen start to listen on a port\n\n```javascript\napp.listen(3000)\n```\n\nThen send a curl get request to /home\n\n### Creating Logging Middleware\n\nMiddlware Documentation: [http://expressjs.com/en/guide/writing-middleware.html](http://expressjs.com/en/guide/writing-middleware.html)\n\nA middleware is a method which is registered with the server to be called prior to all requests are received. \n\nThe middleware should be passed the request and response object and also a method to be called to execute the next middleware.\n\nThe middleware has the opportunity to manipulate the headers, query params and or body prior to the actually request listeners receives it.\n\nIn addition the middleware can inhibit the request from actually proceeding if the content of the request does not meet the requirements for the request at that path.\n\n### Create a middleware which will log the request method, path and all headers which were sent.\n\n```javascript\n\nconst requestLogger = (request, response, next) =\u003e {\n    \n    const method = request.method\n    const path = request.path\n    const query = request.query\n    const headers = request.headers\n\n    console.log(`${method} ${path} ${JSON.stringify(query)}`)\n    \n    Object.keys(headers).forEach(k =\u003e {\n        console.log(`${k}: ${headers[k]}`)\n    })\n    \n    next();\n}\n```\n\nWe now need to register the middleware with the express server prior to the app starting to listen on the port.\n\n\n```javascript\napp.use(requestLogger);\n```\n\nNow run `curl localhost:3000/home` in the command line and you will see the following output.\n\n```bash\nGET /home\nhost: localhost:3000\nuser-agent: curl/7.61.0\naccept: */\n```\n\n### Check Authorisation Header Middleware\n\nSo it can be that some requests require that a access  token of some sort be present within the headers and if it is not present then the user should receive a `401 Unauthorised` statusCode.\n\n```javascript\n\nconst AuthorisationMiddleware = (req, res, next) =\u003e {\n\n    /// check if the headers has a property of authorization\n    /// if not then \n    if(!req.headers.authorization) {\n        res.status(401).send({ message: \"Unauthorised\" })\n    }\n\n    next()\n}\n\napp.use([AuthorisationMiddleware, ...])\n\n```\n\n### Body Parser\n\n### Installing the body-parser library\n\nThe body parser library is a middleware which will convert the form data to a javascript object without you having to do it.\n\n```bash\nnpm install --save-dev body-parser\n```\n\n### Add the urlencoded middleware to express\n\n```javascript\nconst bodyParser = require('body-parser')\napp.use(bodyParser.urlencoded({ extended: true }))\n```\n\nYou can now access the form data as a javascript object directly through the `request.body` property.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidthorn%2Fexpress-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidthorn%2Fexpress-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidthorn%2Fexpress-server/lists"}