Use fs/promises to reply immediately after a message is received

var -> const
Add standard for linting.
This commit is contained in:
Sage Vaillancourt 2024-06-22 14:08:05 -04:00
parent d0653ff184
commit d72d8ed222
6 changed files with 3463 additions and 59 deletions

52
app.js
View File

@ -1,41 +1,39 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const createError = require('http-errors')
const express = require('express')
const path = require('path')
const cookieParser = require('cookie-parser')
const logger = require('morgan')
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
const indexRouter = require('./routes/index')
var app = express();
const app = express()
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/', indexRouter)
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
app.use(function (req, res, next) {
next(createError(404))
})
// error handler
app.use(function(err, req, res, next) {
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
// render the error page
res.status(err.status || 500);
res.render('error');
});
res.status(err.status || 500)
res.render('error')
})
module.exports = app;
module.exports = app

25
bin/www
View File

@ -3,23 +3,20 @@
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('message:server');
var http = require('http');
const app = require('../app')
const debug = require('debug')('message:server')
const http = require('http')
/**
* Get port from environment and store in Express.
*/
var port = normalizePort('3010');
const port = normalizePort('3010')
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
const server = http.createServer(app)
/**
* Listen on provided port, on all network interfaces.
@ -34,7 +31,7 @@ server.on('listening', onListening);
*/
function normalizePort(val) {
var port = parseInt(val, 10);
const port = parseInt(val, 10)
if (isNaN(port)) {
// named pipe
@ -58,9 +55,9 @@ function onError(error) {
throw error;
}
var bind = typeof port === 'string'
const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
: 'Port ' + port
// handle specific listen errors with friendly messages
switch (error.code) {
@ -82,9 +79,9 @@ function onError(error) {
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
const addr = server.address()
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
: 'port ' + addr.port
debug('Listening on ' + bind);
}

3413
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,9 @@
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
"start": "node ./bin/www",
"lint": "standard",
"lint-fix": "standard --fix"
},
"dependencies": {
"cookie-parser": "~1.4.4",
@ -12,5 +14,8 @@
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1"
},
"devDependencies": {
"standard": "^17.1.0"
}
}

View File

@ -1,23 +1,23 @@
const express = require('express');
const router = express.Router();
const fs = require('fs')
const express = require('express')
const router = express.Router()
const fs = require('fs/promises')
const file = './messages'
const get = (res, thankYou) => {
res.render('index', { title: 'Message Sage 😎', thankYou });
res.render('index', { title: 'Message Sage 😎', thankYou })
}
/* GET home page. */
router.get('/', function(req, res, next) {
router.get('/', function (req, res, next) {
get(res, false)
});
})
router.post('/', (req, res) => {
const log = { message: req.body.message, timestamp: new Date() }
fs.appendFileSync(file, JSON.stringify(log) + '\n')
fs.appendFile(file, JSON.stringify(log) + '\n').catch(console.error)
get(res, true)
})
module.exports = router;
module.exports = router

View File

@ -1,9 +0,0 @@
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
module.exports = router;