Coder Social home page Coder Social logo

Example not working about fastify HOT 6 CLOSED

sandro-pasquali avatar sandro-pasquali commented on June 26, 2024
Example not working

from fastify.

Comments (6)

mikko-severo avatar mikko-severo commented on June 26, 2024 1

Hi @DylanPiercey
Thanks. I got it to work w/common js.

`require('@marko/compiler/register');
const fastify = require('fastify')({
logger: false
}),
template=require( "./template.marko"),
// markoPlugin =require("@marko/fastify") ;

// fastify.register(markoPlugin);
// Declare a route
fastify.get('/', function (request, reply) {
let render = template.default.renderToString({}),
res_body = {"view": render };
//return res_body
reply.send(res_body);
})

// Run the server!
fastify.listen({ port: 3000 }, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
// Server is now listening on ${address}
})`

from fastify.

DylanPiercey avatar DylanPiercey commented on June 26, 2024

I believe the issue is that you haven't configured the environment to be able to load Marko templates.

Marko is not understood by node or any environment by default and has to be preprocessed. In node you can use the Marko require hook require("@marko/compiler/register"), but we currently recommend bundling the node server for the best experience. You can see this in many of our boilerplate/example repos with webpack/rollup/vite https://github.com/marko-js/examples/.

I added a note to the docs here about this as well.

from fastify.

mikko-severo avatar mikko-severo commented on June 26, 2024

Hi,

Beginner here, can I use "import "@marko/compiler/register" " to use the Marko required hook?

from fastify.

DylanPiercey avatar DylanPiercey commented on June 26, 2024

@mikko-severo we highly recommend avoiding using the Marko compiler hook. Marko is designed to be used with a bundler since one of the points of Marko is to be able to load just the JS needed for the client.

I'd recommend starting from one of our Vite template projects, eg

npm init marko -- --template vite-fastify

from fastify.

mikko-severo avatar mikko-severo commented on June 26, 2024

@DylanPiercey Thanks for the insight and fast reply. I mainly needed Marko for the server rending (no need for a bundler, I think), but I couldn't just get it to work. I keep getting the error the OP posted when using Fastify w/ESM or CMJ syntax. Although I got it working with Express w/ CMJ syntax by requiring the "Marko/compiler/register."

Any idea how I can get it to work with Fastify for server rendering?

Thanks

from fastify.

DylanPiercey avatar DylanPiercey commented on June 26, 2024

@mikko-severo I still think even if you just want to use Marko for the server you should use a bundler. Here is vite express example which only bundles the server side https://github.com/DylanPiercey/marko-vite-express-ssr-only

Benefit here is that in prod your server startup doesn't need to go through the Marko compiler, and in dev you get cool things like hot reloading for your server code.

It also has an incremental path to using the client side bundler when/if you want to.

from fastify.

Related Issues (4)

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.