ReactJS application ExpressJS application
Please use the following dummy users to access the Node app:
- user [email protected], password mario
- user [email protected], password luigi
- user [email protected], password wario
npm run install-db
This will provide three mock ads to start with.
All configuration values are stored in a .env file, you must copy or rename ".env.example" to ==> ".env" locally, and insert your own environment configuration.
cp .env.example .env
This API relies on my translation NPM package: @feelhippo/translate
This tiny and KISS package allows you to automatically translate your locales from English to 20 other languages.
The package relies on Yandex's Translate API, which for single words and short sentences is just perfect.
Once you have imported it, you only have to provide it with the route to your locale and voila'!
To install it:
$ npm install @feelhippo/translate
Since it will only be used in development, the package is called from "translateLocales.js", located in the root directory of the project, and which should not be used in production.
To translate all of the i18n locales:
// package.json
"scripts": {
"translate": "node translateLocales.js"
}
and then:
$ npm run translate
Now it's time to start the server:
npm start
https://localhost:3000/api/ads
[
{
"tags": [
"First",
"Second"
],
"_id": "5eb1a1f71834e346749a3980",
"name": "String",
"sell": Bool,
"price": Number,
"picture": "String",
"__v": 0
},
...
]
https://localhost:3000/api/ads
await axios.post('https://localhost:3000/api/ads', {
name: req.body.name,
sell: req.body.sell,
price: req.body.price,
picture: req.file.filename,
tags: [req.body.tag1, req.body.tag2]
});
I have used Multer, a node.js middleware for handling multipart/form-data, to handle picture uploads. The middleware renames the document, and stores it inside public/images. When an ad is added with a POST request.
https://localhost:3000/deleteAd/:id
The middleware uses a JWT Authorization token to validate the request, so that only registered user are allowed to delete ads.
app.use('/deleteAd/:id', jwtAuth(), require('./routes/api/deleteAd'));