Skip to content

There is no need to use express.static() for static-files sample #889

@mrdulin

Description

@mrdulin

Here is my sample:

server.js

const express = require('express');
const path = require('path');
const package = require('./package.json');

function createServer() {
  const app = express();
  app.set('views', path.resolve(__dirname, './views'));
  app.set('view engine', 'ejs');

  // app.use('/static', express.static(path.resolve(__dirname, 'public')));

  app.get('/', (req, res) => {
    res.render('index');
  });

  app.get('/version', (req, res) => {
    res.send(`version: ${package.version}`);
  });

  const PORT = process.env.PORT || 8080;
  return app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}...`);
  });
}

createServer();

./views/index.ejs

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="static/index.css" />
    <title>GAE Static File</title>
  </head>
  <body>
    <h1 class="red">GAE Static File</h1>
  </body>
</html>

app.yaml:

service: static-files
runtime: nodejs8
handlers:
  - url: /static
    static_dir: public

works fine.

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions