I would like to create an app in koa that would render react components and serve html file. Most exactily, i want to add React in simple HTML but when i use my app i can read my html file but not my react component.
I have a Koa.js server
require('isomorphic-fetch');
const Koa = require('koa');
const dotenv = require('dotenv');
const serve = require('koa-static');
const logger = require('koa-logger');
dotenv.config();
const server = new Koa();
const port = parseInt(process.env.PORT, 10) || 8000;
router = require(./router);
server.use(serve('./public'))
.use(logger());
.use(router.routes())
.use(router.allowedMethods());
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
In my public folder, i have a html and css file. For example :
<head>
<!-- Note: when deploying, replace "development.js" with "production.min.js". -->
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="/react-component/date-picker.js"></script>
</head>
<body>
<p>You can look a React components :) :</p>
<div id = 'Date'></p>
</body>
And finally I have a folder which all React components are present ( react-component ) and one of my React Component :
import React from "react";
import ReactDOM from "react-dom";
import {AppProvider} from "@shopify/polaris";
import en from '@shopify/polaris/locales/en.json';
import "@shopify/polaris/dist/styles.css";
import {useCallback, useState} from 'react';
import {DatePicker} from '@shopify/polaris';
function DatePickerExample(props) {
const [{month, year}, setDate] = useState({month: 1, year: 2018});
const [selectedDates, setSelectedDates] = useState({
start: new Date('Wed Feb 07 2018 00:00:00 GMT-0500 (EST)'),
end: new Date('Mon Mar 12 2018 00:00:00 GMT-0500 (EST)'),
});
const handleMonthChange = useCallback(
(month, year) => setDate({month, year}),
[],
);
return (
<DatePicker
month={month}
year={year}
onChange={setSelectedDates}
onMonthChange={handleMonthChange}
selected={selectedDates}
multiMonth
allowRange
/>
);
}
const domContainer = document.getElementById('date');
ReactDOM.render(e(DatePickerExample), domContainer);
I think a problem with koa-static because with koa-logger, i see :
<-- GET /react-component/date-picker.js
--> GET /react-component/date-picker.js 404 0ms
But I don't know how to fix this problem. Thank you everyone :)