Skip to content

Commit fe6d92a

Browse files
committed
guest book
1 parent 8c364f2 commit fe6d92a

File tree

7 files changed

+617
-38
lines changed

7 files changed

+617
-38
lines changed
Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
1-
/// <reference path="./typings/tsd.d.ts" />
2-
console.log(require('url').parse('http://localhost:3000/1?p=2').pathname);
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var http_1 = require("http");
4+
var path_1 = require("path");
5+
var body_parser_1 = require("body-parser");
6+
var express = require("express");
7+
var logger = require("morgan");
8+
var app = express();
9+
var entries = [];
10+
app.locals.entries = entries;
11+
app.use(logger("dev"));
12+
app.set("views", path_1.resolve(__dirname, "views"));
13+
app.set("view engine", "ejs");
14+
app.use(body_parser_1.urlencoded({ extended: false }));
15+
app.get("/", function (req, res) {
16+
res.render("index");
17+
});
18+
app.get("/new-entry", function (req, res) {
19+
res.render("new-entry");
20+
});
21+
app.post("/new-entry", function (req, res) {
22+
if (!req.body.title || !req.body.body) {
23+
res.status(400).send("Entries must have a title and a body.");
24+
return;
25+
}
26+
entries.push({
27+
title: req.body.title,
28+
body: req.body.body,
29+
published: new Date()
30+
});
31+
res.redirect("/");
32+
});
33+
app.use(function (req, res) {
34+
res.status(404).render("404");
35+
});
36+
http_1.createServer(app).listen(3000, function () {
37+
console.log("Guestbook app started on port 3000.");
38+
});
Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
1-
/// <reference path="./typings/tsd.d.ts" />
21

3-
import url = require('url');
4-
console.log(require('url').parse('http://localhost:3000/1?p=2').pathname);
2+
import { createServer } from "http";
3+
import { resolve } from "path";
4+
import { urlencoded } from "body-parser";
55

6+
import * as express from "express";
7+
import * as logger from "morgan";
68

9+
let app = express();
710

11+
let entries = [];
12+
app.locals.entries = entries;
813

14+
app.use(logger("dev"));
15+
16+
app.set("views", resolve(__dirname, "views"));
17+
app.set("view engine", "ejs");
18+
19+
app.use(urlencoded({ extended: false }));
20+
21+
app.get("/", (req, res) => {
22+
res.render("index");
23+
});
24+
25+
app.get("/new-entry", (req, res) => {
26+
res.render("new-entry");
27+
});
28+
29+
app.post("/new-entry", (req, res) => {
30+
if (!req.body.title || !req.body.body) {
31+
res.status(400).send("Entries must have a title and a body.");
32+
return;
33+
}
34+
entries.push({
35+
title: req.body.title,
36+
body: req.body.body,
37+
published: new Date()
38+
});
39+
res.redirect("/");
40+
});
41+
42+
app.use((req, res) => {
43+
res.status(404).render("404");
44+
});
45+
46+
createServer(app).listen(3000, function () {
47+
console.log("Guestbook app started on port 3000.");
48+
});

step03_chapter03_foundations_of_express/guestbook/converter.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

step03_chapter03_foundations_of_express/guestbook/converter.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)