Skip to content

Commit

Permalink
express examples
Browse files Browse the repository at this point in the history
  • Loading branch information
scc36 committed Oct 11, 2014
1 parent de8ed37 commit 5055817
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 7 deletions.
9 changes: 6 additions & 3 deletions ex5-serverExpress.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Use NPM to install expressjs
//npm install express
/* npm install express
* In case it breaks, choose an earlier version, like:
* npm install [email protected]
*/

// Set express module to variable "express"
var express = require('express');
var express = require("express");

// Create an "app" variable from the express library
var app = express();

// Add a "route" that leads to the req/res, as seen in Node.js
app.get('/', function(req, res){
app.get("/", function(req, res){
res.send("Express powered web page");
});

Expand Down
44 changes: 42 additions & 2 deletions ex6-routesServer.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
// Outputs "Hello World" to the console
console.log("Hello World");
// Express makes routes easy

// Set express module to variable "express"
var express = require("express");

// Create an "app" variable from the express library
var app = express();

// Add a "route" that leads to the req/res, as seen in Node.js
app.get("/", function(req, res){
res.send("Express powered web page");
});

// Second route to /page
app.get("/page", function(req, res){
res.send("This is a second page");
});

// This page returns a JSON value, the standard Javascript data sructure
app.get("/json", function(req, res){
res.json(
{
"message": "welcome to the page"
}
)
});

// Visit page with value set
// /info?value=my value
app.get("/info", function(req, res){
res.send("The info you sent is: " + req.query.value);
});

// This page is requires a post call: such as from a web frm
app.post("/form", function(req, res){
res.send("The form value is: " + req.query.value);
});

// Starts up the server
var server = app.listen(1337, function() {

});
23 changes: 21 additions & 2 deletions ex7-staticFolder.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
// Outputs "Hello World" to the console
console.log("Hello World");
// Express makes routes easy

// Set express module to variable "express"
var express = require("express");

// Create an "app" variable from the express library
var app = express();

// Sets the /static webpage route to the /public folder
app.use('/static', express.static(__dirname + '/public'));

// Add a "route" that leads to the req/res, as seen in Node.js
app.get("/", function(req, res){
// Grab a file and respond with that
res.sendFile(__dirname + '/public/index.html');
});

// Starts up the server
var server = app.listen(1337, function() {

});
File renamed without changes.
16 changes: 16 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Example Web Page</title>
</head>
<body>
<h1>My Webpage</h1>
<p>Here it is</p>

<!--- Note that /static is used, even if the picture is in /public --->
<img src="/static/makerbar.jpg" alt="Makerbar picture">
</body>
</html>
Binary file added public/makerbar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5055817

Please sign in to comment.