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

50
app.js
View File

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

25
bin/www
View File

@ -3,23 +3,20 @@
/** /**
* Module dependencies. * Module dependencies.
*/ */
const app = require('../app')
var app = require('../app'); const debug = require('debug')('message:server')
var debug = require('debug')('message:server'); const http = require('http')
var http = require('http');
/** /**
* Get port from environment and store in Express. * Get port from environment and store in Express.
*/ */
const port = normalizePort('3010')
var port = normalizePort('3010');
app.set('port', port); app.set('port', port);
/** /**
* Create HTTP server. * Create HTTP server.
*/ */
const server = http.createServer(app)
var server = http.createServer(app);
/** /**
* Listen on provided port, on all network interfaces. * Listen on provided port, on all network interfaces.
@ -34,7 +31,7 @@ server.on('listening', onListening);
*/ */
function normalizePort(val) { function normalizePort(val) {
var port = parseInt(val, 10); const port = parseInt(val, 10)
if (isNaN(port)) { if (isNaN(port)) {
// named pipe // named pipe
@ -58,9 +55,9 @@ function onError(error) {
throw error; throw error;
} }
var bind = typeof port === 'string' const bind = typeof port === 'string'
? 'Pipe ' + port ? 'Pipe ' + port
: 'Port ' + port; : 'Port ' + port
// handle specific listen errors with friendly messages // handle specific listen errors with friendly messages
switch (error.code) { switch (error.code) {
@ -82,9 +79,9 @@ function onError(error) {
*/ */
function onListening() { function onListening() {
var addr = server.address(); const addr = server.address()
var bind = typeof addr === 'string' const bind = typeof addr === 'string'
? 'pipe ' + addr ? 'pipe ' + addr
: 'port ' + addr.port; : 'port ' + addr.port
debug('Listening on ' + bind); 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", "version": "0.0.0",
"private": true, "private": true,
"scripts": { "scripts": {
"start": "node ./bin/www" "start": "node ./bin/www",
"lint": "standard",
"lint-fix": "standard --fix"
}, },
"dependencies": { "dependencies": {
"cookie-parser": "~1.4.4", "cookie-parser": "~1.4.4",
@ -12,5 +14,8 @@
"express": "~4.16.1", "express": "~4.16.1",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"morgan": "~1.9.1" "morgan": "~1.9.1"
},
"devDependencies": {
"standard": "^17.1.0"
} }
} }

View File

@ -1,23 +1,23 @@
const express = require('express'); const express = require('express')
const router = express.Router(); const router = express.Router()
const fs = require('fs') const fs = require('fs/promises')
const file = './messages' const file = './messages'
const get = (res, thankYou) => { const get = (res, thankYou) => {
res.render('index', { title: 'Message Sage 😎', thankYou }); res.render('index', { title: 'Message Sage 😎', thankYou })
} }
/* GET home page. */ /* GET home page. */
router.get('/', function (req, res, next) { router.get('/', function (req, res, next) {
get(res, false) get(res, false)
}); })
router.post('/', (req, res) => { router.post('/', (req, res) => {
const log = { message: req.body.message, timestamp: new Date() } 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) 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;