Express is a framework for making HTTP servers with node
A "route" is how the server responds to a specific request based on the request's "path"
Here is a basic Express server which responds with "Hello World" when the client requests the path "/":
var express = require('express') var app = express() app.get('/', function (req, res) { res.send('Hello World!') }) app.listen(3000, function () { console.log('Example app listening on port 3000!') })http://IP:3000/
Of course a route can be for any "path", here is an example for "/somethingelse":
app.get('/somethingelse', function (req, res) { res.send('Hello World!'); });
Express can be configured to serve static files as well as routes. In this example any file in the "public" directory will be served if it's name is requested:
app.use(express.static('public'));
If you would like to dynamically serve a file based upon a request to a "route" you can use the "sendFile" function:
app.get('/randomfile', function (req, res) { var fileToSend = "somerandomfile.txt"; res.sendfile(fileToSend, {root: './public'}); // Files inside "public" folder });
<form> </form>
<input type="text" name="textfield" value="The Value" />Allows text to be entered. The "value" attribute can be used to specify a default value.
<input type="hidden" name="hiddenfield" value="The Value" />> Allows a value to be in the form but not displayed to the user. The "value" attribute specifies the value.
<input type="button" name="pushbutton" value="Button" />Button that can be pressed by the user. The "value" attribute is what displays on the page. Generally this is used to trigger an event that will call some JavaScript.
<input type="submit" name="submitbutton" value="Submit" />A button that is used to submit the form to the server. The "value" attribute is what display inside the button.
<input type="reset" name="resetbutton" value="Reset" />A button that is used to reset the form to the state it was when loaded. The "value" attribute is what display inside the button.
<input type="radio" name="radiobutton" value="Radio 1" /> <input type="radio" name="radiobutton" value="Radio 2" />A series of radio buttons that allow the user to choose between a series of options. Each one has the same "name" attribute but a different "value" attribute.
<select name="selectlist"> <option value="option1">Option 1</option> <option value="option2">Option 2</option> </select>A drop down list of options that allows the user to select one. Multiple selections can be enabled by adding the keyword "multiple" in the "select" tag.
<input type="color" name="somecolor">Allows the user to select a color using a standard color picker.
<input type="range" name="arange" min="0" max="10">Allows the user to input a possible range of values.
In order for a form to submit information to a server for processing, we need to use the "action" and "method" attributes.
Using method="POST" on a form sends the data along with the request to the server behind the scenes (with the HTTP headers) and isn't visible to the user so it can't be book marked or otherwise saved. It might be a bit more private in this manner.
Using method="GET" on a form sends the data "url encoded" as a query string to the server which displays on the URL. You would want to use this for search results that are to be shared or bookmarked for later retrieval.
The "action" attribute is where to send the form data when the submit button is pressed.
<form method="GET" action="/processit"> <input type="text" name="textfield" value="The Value" />
<input type="submit" name="submitbutton" value="Submit" />
</form>
Express receives the data from the form as part of the request. You have to make a "route" for "post" if the method is post or "get" if the method is get.
First we need to install a piece of "middleware" to work with POST data in Express. The body-parser will take care of parsing the post as it comes in making the variables available to us in the the "route":
npm install body-parserat the top of our server.js we need to add these lines (after "var app = express()")
var bodyParser = require('body-parser'); var urlencodedParser = bodyParser.urlencoded({ extended: true }); // for parsing form data app.use(urlencodedParser);
Now, each "name" from the form elements will come in as part of the req.body object.
app.post('/processit', function(req, res) { var textvalue = req.body.textfield; res.send("You submitted: " + textvalue); });
If a form is submitted with a method="GET" the data will come in on the query string and can be parsed as follows:
app.get('/processit', function(req, res) { var textvalue = req.query.textfield; res.send("You submitted: " + textvalue); });
NPM is the node package manager (being extended to JavaScript in general). It is the common way to install any node package which enhances the functionality of node.
Search for Packages and install:
npm install package_name