Skip to content

Commit 7d3e019

Browse files
committed
feat: 최초 커밋 - nodejs + express server boilerplate
0 parents  commit 7d3e019

File tree

18 files changed

+2833
-0
lines changed

18 files changed

+2833
-0
lines changed

.gitignore

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
*.env
59+
60+
# next.js build output
61+
.next

app.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var config = require('./src/config');
2+
3+
var createError = require('http-errors');
4+
var express = require('express');
5+
var path = require('path');
6+
var cookieParser = require('cookie-parser');
7+
var logger = require('morgan');
8+
9+
var apiRouter = require('./src/api');
10+
11+
const cLogger = require('./src/lib/logger');
12+
13+
var app = express();
14+
15+
// ajax cross domain issue open
16+
const cors = require('cors')();
17+
app.use(cors);
18+
19+
20+
// view engine setup
21+
app.set('views', path.join(__dirname, 'src/views'));
22+
app.set('view engine', 'ejs');
23+
24+
app.use(logger('dev'));
25+
app.use(express.json());
26+
app.use(express.urlencoded({ extended: false }));
27+
app.use(cookieParser(config.secrets.secretKey));
28+
29+
// rest api 서버 경로 설정
30+
app.use(express.static(path.join(__dirname, 'public')));
31+
32+
33+
app.use('/', apiRouter);
34+
35+
// catch 404 and forward to error handler
36+
app.use(function(req, res, next) {
37+
next(createError(404));
38+
});
39+
40+
// error handler
41+
app.use(function (err, req, res, next) {
42+
// set locals, only providing error in development
43+
res.locals.message = err.message;
44+
res.locals.error = req.app.get('env') === 'development' ? err : {};
45+
// render the error page
46+
res.status(err.status || 500);
47+
res.json(err);
48+
});
49+
50+
// Handle uncaughtException
51+
process.on('uncaughtException', function (err) {
52+
// debug('Caught exception: %j', err);
53+
// cLogger.logNormal('error', err);
54+
console.log('uncaughtException:', err);
55+
process.exit(1);
56+
});
57+
58+
module.exports = app;

bin/www

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
const sprintf = require('sprintf-js').sprintf;
7+
const config = require('../src/config');
8+
const logger = require('../src/lib/logger');
9+
10+
var app = require('../app');
11+
var debug = require('debug')(`${config.basic.name}:server`);
12+
13+
/**
14+
* Get port from environment and store in Express.
15+
*/
16+
17+
const port = config.basic.port;
18+
app.set('port', port);
19+
20+
var server = null;
21+
22+
/**
23+
* Create HTTP server.
24+
*/
25+
server = require('http').createServer(app);
26+
27+
/**
28+
* Listen on provided port, on all network interfaces.
29+
*/
30+
server.listen(port);
31+
server.on('error', onError);
32+
server.on('listening', onListening);
33+
34+
/* ===== fixme:종료일괄처리필요할경우 ===== */
35+
// server.on('close', function onServerClose() {
36+
// console.log('Stopping ...');
37+
// logger.logServEnd(config);
38+
// db.close();
39+
// });
40+
41+
// process.on('SIGINT', function onSigint() {
42+
// server.close();
43+
// });
44+
45+
/**
46+
* Event listener for HTTP server "error" event.
47+
*/
48+
function onError(error) {
49+
if (error.syscall !== 'listen') {
50+
throw error;
51+
}
52+
53+
var bind = typeof port === 'string'
54+
? 'Pipe ' + port
55+
: 'Port ' + port;
56+
57+
// handle specific listen errors with friendly messages
58+
switch (error.code) {
59+
case 'EACCES':
60+
console.error(bind + ' requires elevated privileges');
61+
process.exit(1);
62+
break;
63+
case 'EADDRINUSE':
64+
console.error(bind + ' is already in use');
65+
process.exit(1);
66+
break;
67+
default:
68+
throw error;
69+
}
70+
}
71+
72+
/**
73+
* Event listener for HTTP server "listening" event.
74+
*/
75+
76+
function onListening() {
77+
var addr = server.address();
78+
79+
logger.logServStart(config, sprintf('%s:%s', addr.address, addr.port));
80+
81+
var bind = typeof addr === 'string'
82+
? 'pipe ' + addr
83+
: 'port ' + addr.port;
84+
debug('Listening on ' + bind);
85+
}

0 commit comments

Comments
 (0)