35
loading...
This website collects cookies to deliver better user experience
npx create-next-app@latest
, but the source code can be found here - https://github.com/pjeziorowski/gallery-demo/tree/master/frontendDockerfile
FROM node:alpine
RUN mkdir -p /usr/src
WORKDIR /usr/src
COPY . /usr/src
RUN npm install
RUN npm run build
EXPOSE 3000
CMD npm run start
Adding a query to our backend (which we will be built soon in the next steps) that fetches a list of images to display in our gallery
function useImages() {
return useQuery("images", async () => {
const { data } = await axios.get(
`${apiRoot}/api/v1/images`
);
return data;
});
}
Plus, we adjusted the HTML and styling for the demo purpose of showing a list of images
Preview Environment
feature before merging the pull request to our production environmentnpx express-generator --no-view
, and the source code can be found here - https://github.com/pjeziorowski/gallery-demo/tree/master/backendAdding a Dockerfile
FROM node:16
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "src/index.js" ]
Creating a /api/v1/images
endpoint that returns a hardcoded array of images
router.get('/images', (req, res) => {
res.json([
{
title: 'IMG_4985.HEIC',
size: '3.9 MB',
source:
'https://images.unsplash.com/photo-1582053433976-25c00369fc93?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=512&q=80',
}
});
});
In the next step we will improve the function to use a Mongo database instead.
production
environment.Add my first application
, select a correct repository, Docker
as build mode and expose port 3000
. The application root path is /frontend
.MongoDB
database - it will be used by our backend later on. You can do so by clicking on Add
button in Qovery Console in Environment.Add
→ Application
, pick up /backend
as application root path, 8080
port, and Docker
build mode.DATABASE_URL
that points to our Mongo database internal URL in our backend Environment Variable
settings:API_ROOT
in our frontend application that points to our backend external URL:Open
- you should be redirected to the image galleryEnvironment
→ Settings
→ Preview Env
and tick it for the backend appconst databaseUrl = process.env.DATABASE_URL
|| 'mongodb://localhost:27017/test';
const imageSchema = new mongoose.Schema({
title: String,
size: String,
source: String
});
mongoose.connect(databaseUrl);
router.get('/', (req, res) => {
imageSchema.find().then((data) => {
res.json(
data
)
});
});
Open
- in the image gallery, you will see an empty list because we don't yet have any images in the database.db.createCollection("images")
db.images.insert([
{
title: 'IMG_4985.HEIC',
size: '3.9 MB',
source:
'https://images.unsplash.com/photo-1582053433976-25c00369fc93?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=512&q=80',
},
{
title: 'IMG_4985.HEIC',
size: '3.9 MB',
source:
'https://images.unsplash.com/photo-1582053433976-25c00369fc93?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=512&q=80',
},
{
title: 'IMG_4985.HEIC',
size: '3.9 MB',
source:
'https://images.unsplash.com/photo-1582053433976-25c00369fc93?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=512&q=80',
}
])