-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (36 loc) · 1.17 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const express = require( 'express' );
const sharp = require( 'sharp' );
const path = require( 'path' );
// create express application
const app = express();
// handle `/` request
app.get( '/', ( req, res ) => {
res.sendFile( path.resolve( __dirname, 'www/index.html' ) );
} );
// handle `/resize` request
app.get( '/resize/:size', ( req, res ) => {
// get size
const [ _width, _height ] = req.params.size.split( "x" );
// get Number width and height
const width = Number( _width ) || null; // fallback to `null`
const height = Number( _height ) || null; // fallback to `null`
// send response content-type
res.contentType( 'image/png' );
// resize image and send
const pngStream = sharp( path.resolve( __dirname, 'lenna.png' ) )
.resize( width, height, {
fit: width && height ? 'fill' : 'cover',
} )
.png()
.toBuffer()
.then( ( buffer ) => {
res.send( buffer );
} );
} );
// handle web assets
app.use( '/www', express.static( path.resolve( __dirname, 'www' ) ) );
// listen on a port
const port = process.env.PORT || 80;
app.listen( port, () => {
console.log( `Server started on port ${ port }.` );
} );