김재현

Express 기본 라우터 설정

1 +var express = require('express');
2 +var path = require('path');
3 +var favicon = require('serve-favicon');
4 +var logger = require('morgan');
5 +var cookieParser = require('cookie-parser');
6 +var bodyParser = require('body-parser');
7 +var stylus = require('stylus');
8 +
9 +var index = require('./routes/index');
10 +var users = require('./routes/users');
11 +
12 +var app = express();
13 +
14 +// view engine setup
15 +app.set('views', path.join(__dirname, 'views'));
16 +app.set('view engine', 'ejs');
17 +
18 +// uncomment after placing your favicon in /public
19 +//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
20 +app.use(logger('dev'));
21 +app.use(bodyParser.json());
22 +app.use(bodyParser.urlencoded({ extended: false }));
23 +app.use(cookieParser());
24 +app.use(stylus.middleware(path.join(__dirname, 'public')));
25 +app.use(express.static(path.join(__dirname, 'public')));
26 +
27 +app.use('/', index);
28 +app.use('/users', users);
29 +
30 +// catch 404 and forward to error handler
31 +app.use(function(req, res, next) {
32 + var err = new Error('Not Found');
33 + err.status = 404;
34 + next(err);
35 +});
36 +
37 +// error handler
38 +app.use(function(err, req, res, next) {
39 + // set locals, only providing error in development
40 + res.locals.message = err.message;
41 + res.locals.error = req.app.get('env') === 'development' ? err : {};
42 +
43 + // render the error page
44 + res.status(err.status || 500);
45 + res.render('error');
46 +});
47 +
48 +module.exports = app;
49 +
50 +/*
51 +var mysql = require('mysql');
52 +var connection = mysql.createConnection({
53 + host : 'localhost',
54 + user : 'admin',
55 + password : 'admin',
56 + database : 'ossprojectdb'
57 +});
58 +
59 +
60 +connection.connect();
61 +
62 +connection.query('SELECT * from users', function(err, rows, fields) {
63 + if (!err)
64 + console.log('The solution is: ', rows);
65 + else
66 + console.log('Error while performing Query.');
67 +});
68 +
69 + connection.end();
70 +*/
1 +#!/usr/bin/env node
2 +
3 +/**
4 + * Module dependencies.
5 + */
6 +
7 +var app = require('../app');
8 +var debug = require('debug')('open-source-project-2017-02:server');
9 +var http = require('http');
10 +
11 +/**
12 + * Get port from environment and store in Express.
13 + */
14 +
15 +var port = normalizePort(process.env.PORT || '3000');
16 +app.set('port', port);
17 +
18 +/**
19 + * Create HTTP server.
20 + */
21 +
22 +var server = http.createServer(app);
23 +
24 +/**
25 + * Listen on provided port, on all network interfaces.
26 + */
27 +
28 +server.listen(port);
29 +server.on('error', onError);
30 +server.on('listening', onListening);
31 +
32 +/**
33 + * Normalize a port into a number, string, or false.
34 + */
35 +
36 +function normalizePort(val) {
37 + var port = parseInt(val, 10);
38 +
39 + if (isNaN(port)) {
40 + // named pipe
41 + return val;
42 + }
43 +
44 + if (port >= 0) {
45 + // port number
46 + return port;
47 + }
48 +
49 + return false;
50 +}
51 +
52 +/**
53 + * Event listener for HTTP server "error" event.
54 + */
55 +
56 +function onError(error) {
57 + if (error.syscall !== 'listen') {
58 + throw error;
59 + }
60 +
61 + var bind = typeof port === 'string'
62 + ? 'Pipe ' + port
63 + : 'Port ' + port;
64 +
65 + // handle specific listen errors with friendly messages
66 + switch (error.code) {
67 + case 'EACCES':
68 + console.error(bind + ' requires elevated privileges');
69 + process.exit(1);
70 + break;
71 + case 'EADDRINUSE':
72 + console.error(bind + ' is already in use');
73 + process.exit(1);
74 + break;
75 + default:
76 + throw error;
77 + }
78 +}
79 +
80 +/**
81 + * Event listener for HTTP server "listening" event.
82 + */
83 +
84 +function onListening() {
85 + var addr = server.address();
86 + var bind = typeof addr === 'string'
87 + ? 'pipe ' + addr
88 + : 'port ' + addr.port;
89 + debug('Listening on ' + bind);
90 +}
1 +{
2 + "name": "ossproject-2017-02",
3 + "version": "1.0.0",
4 + "description": "Tour Information Stamp Application",
5 + "main": "app.js",
6 + "private": true,
7 + "scripts": {
8 + "start": "node ./bin/www",
9 + "test": "echo \"Error: no test specified\" && exit 1"
10 + },
11 + "repository": {
12 + "type": "git",
13 + "url": "http://khuhub.khu.ac.kr/2013104063/open-source-project-2017-02.git"
14 + },
15 + "dependencies": {
16 + "body-parser": "~1.18.2",
17 + "cookie-parser": "~1.4.3",
18 + "debug": "~2.6.9",
19 + "ejs": "~2.5.7",
20 + "express": "~4.15.5",
21 + "morgan": "~1.9.0",
22 + "serve-favicon": "~2.4.5",
23 + "stylus": "0.54.5",
24 + "mysql": "^2.5.4"
25 + },
26 + "author": "",
27 + "license": "ISC"
28 +}
1 +body {
2 + padding: 50px;
3 + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4 +}
5 +a {
6 + color: #00b7ff;
7 +}
1 +body
2 + padding: 50px
3 + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif
4 +a
5 + color: #00B7FF
1 +var express = require('express');
2 +var router = express.Router();
3 +
4 +/* GET home page. */
5 +router.get('/', function(req, res, next) {
6 + res.render('index', { title: 'Express' });
7 +});
8 +
9 +module.exports = router;
1 +var express = require('express');
2 +var router = express.Router();
3 +
4 +/* GET users listing. */
5 +router.get('/', function(req, res, next) {
6 + res.send('respond with a resource');
7 +});
8 +
9 +module.exports = router;
1 +<h1><%= message %></h1>
2 +<h2><%= error.status %></h2>
3 +<pre><%= error.stack %></pre>
1 +<!DOCTYPE html>
2 +<html>
3 + <head>
4 + <title><%= title %></title>
5 + <link rel='stylesheet' href='/stylesheets/style.css' />
6 + </head>
7 + <body>
8 + <h1><%= title %></h1>
9 + <p>Welcome to <%= title %></p>
10 + </body>
11 +</html>