-
Notifications
You must be signed in to change notification settings - Fork 340
Express App Param Pam Pam
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Medium Website | E-Mail
This exercise is about using URL parameters. For example, if you have /message/526aa677a8ceb64569c9d4fb, then you should know how to extract that value which is an ID of the message.
Create an Express.js server that processes PUT /message/:id requests and produces a SHA-1 hash of the current date combined with the ID from the URL.
For instance, if the server receives
PUT /message/526aa677a8ceb64569c9d4fb
it will respond with a hash of the current date (as a string) and the ID.
The SHA-1 can be computed like this:
require('crypto')
.createHash('sha1')
.update(new Date().toDateString() + id)
.digest('hex')
Express.js apps can also be mounted to paths that contain a variable by prepending a : to the beginning of a variable name. For instance, in the following, app handles PUT requests in any subdirectory of /path/:
app.put('/path/:NAME', function(req, res){ /* ... */ });
The given variable is then stored in req.params. So, to extract parameters from within the request handlers, use:
req.params.NAME
You can use req.param middleware to parse the URL parameter.
For example,
app.param('id', function (req, res, next, id) {
req.id = id
...
next()
})
app.get('/message/:id', function (req, res, next) {
console.log(req.id)
next()
})
var express = require('express');
var app = express();
app.put('/message/:NAME', function(req, res) {
var id = req.params.NAME;
res.send(require('crypto')
.createHash('sha1')
.update(new Date().toDateString() + id)
.digest('hex')
);
});
app.listen(process.argv[2]);
var express = require('express')
var app = express()
app.put('/message/:id', function(req, res) {
var id = req.params.id
var str = require('crypto')
.createHash('sha1')
.update(new Date().toDateString() + id)
.digest('hex')
res.send(str)
})
app.listen(process.argv[2])
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3