Giter VIP home page Giter VIP logo

Comments (4)

gpbl avatar gpbl commented on June 20, 2024 1

@opportunato babel is not the problem here – you are trying to require an image directly from node, which doesn't work because you must require a js modules (indeed node is trying to read the JPG as javascript).

In this case, just use the relative url:

this.images.push('/assets/images/fb.jpg');

To make express to serve the content in assets, you need to configure it in server.js:

import path from "path"
// snip...
const assetsDir = path.resolve(__dirname, "assets");
server.use("/assets", express.static(assetsDir, { maxAge: 365 * 24 * 60 * 60 } ));

Note that i've set a max-age header, so browser will cache the image. If you need to skip the cache because you update the image, you could just do

this.images.push('/assets/images/fb.jpg?v=1.1');

from isomorphic500.

opportunato avatar opportunato commented on June 20, 2024

After including this code in server.js, I've started getting this error:

/Users/opportunato/projects/grabr-web-client/node_modules/express/lib/router/index.js:446
      throw new TypeError('Router.use() requires middleware function but got a
            ^
TypeError: Router.use() requires middleware function but got a Object
    at Function.<anonymous> (/Users/opportunato/projects/grabr-web-client/node_modules/express/lib/router/index.js:446:13)
    at Array.forEach (native)
    at Function.use (/Users/opportunato/projects/grabr-web-client/node_modules/express/lib/router/index.js:444:13)
    at EventEmitter.<anonymous> (/Users/opportunato/projects/grabr-web-client/node_modules/express/lib/application.js:216:21)
    at Array.forEach (native)
    at EventEmitter.use (/Users/opportunato/projects/grabr-web-client/node_modules/express/lib/application.js:213:7)
    at Object.<anonymous> (/Users/opportunato/projects/grabr-web-client/src/server.js:28:8)
    at Module._compile (module.js:460:26)
    at normalLoader (/Users/opportunato/projects/grabr-web-client/node_modules/babel-core/lib/babel/api/register/node.js:150:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/opportunato/projects/grabr-web-client/node_modules/babel-core/lib/babel/api/register/node.js:163:7)

Also, shouldn't every asset you require go through the webpack pipeline? Isn't it the main idea behind every build system?

from isomorphic500.

gpbl avatar gpbl commented on June 20, 2024

Yeah sorry there was a misplaced parenthesis in the middleware, I've updated my comment :) anyway check the express documentation for serving static files.

As for the build thing, there's no need to build the server-side code: webpack here is used only to build the files that run on the client. In your case, your require won't be parsed by webpack.

from isomorphic500.

opportunato avatar opportunato commented on June 20, 2024

Thank you very much, everything is clear now!

from isomorphic500.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.