LoginSignup
7
2

More than 5 years have passed since last update.

静的ファイルを配信するだけのシンプルな Node.js + Express

Posted at

使いどころ

  • 静的ファイルをウェブ配信する簡易サーバが欲しいときに。
app.js
'use strict';

// required packages
const path = require('path');

// base
const express = require('express');
const app = express();
app.set('port', 1337);

// static files
app.use(express.static(path.join(__dirname, './wav')));

// server
const http = require('http');
const server = http.createServer(app);

// http 404 error handler
app.use(function (req, res, next) {
    const err = new Error('errorMessage.urlNotFound');
    err.status = 404;
    next(err);
});

// error handling middleware should be loaded after the loading the routes
if ('development' === app.get('env')) {
    const errorHandler = require('errorhandler');
    app.use(errorHandler());
}

// listen
server.listen(app.get('port'));
package.json
{
  "name": "★★★",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "start": "node app"
  }
}
npm install --save express errorhandler http path
7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2