The following resources contain additional information on React Native. Please use them to get more in-depth knowledge on this.
Useful Links on React Native
- React Native − Website Reference for React Native
create-react-app
:# If you haven't installed create-react-app:
# npm install -g create-react-app
create-react-app continious-integration-app
touch Dockerfile
# The Node version that we'll be running for our version of React.
# You may have to search the Node directory for a version that fits
# the version of React you're using.
FROM node:8.9-alpine
# Create a work directory and copy over our dependency manifest files.
RUN mkdir /app
WORKDIR /app
COPY /src /app/src
COPY ["package.json", "package-lock.json*", "./"]
# If you're using yarn:
# yarn build
RUN npm install --production --silent && mv node_modules ../
# Expose PORT 3000 on our virtual machine so we can run our server
EXPOSE 3000
docker-compose
. This tool allows us to orchestrate multiple containers and configurations to build out a Docker image that's just right for our current needs.# docker-compose.yml
version: '3.4'
services:
web:
build:
context: .
dockerfile: Dockerfile
environment:
- NODE_ENV=production
command: npm start
ports:
- 3000:3000
volumes:
- .:/app
docker-compose.yml
file, check out Docker's documentation on Compose.docker-compose build web
docker-compose up web
npm start
and kick off our React server. Once up and running, we should be able to access our application from http://localhost:3000 just like we normally would for a create-react-app
instance!docker-compose
will be doing most of the heavy lifting for us here.# production.yml
version: '3.4'
services:
web:
build:
context: .
dockerfile: Dockerfile-production
environment:
- NODE_ENV=production
docker-compose.yml
file:# docker-compose.yml
version: '3.4'
services:
web:
command: npm start
ports:
- 3000:3000
volumes:
- .:/app
docker-compose
to build our containers. We'll now use the command:docker-compose -f docker-compose.yml -f production.yml build web
docker-compose build web
. However, we now have the advantage of replacing whatever .yml configuration we want depending on our environment. We'll often have to create a new Dockerfile to accurately reflect some of these environmental settings.# The Node version that we'll be running for our version of React.
# You may have to search the Node directory for a version that fits
# the version of React you're using.
FROM node:8.9-alpine
# Create a work directory and copy over our dependency manifest files.
RUN mkdir /app
WORKDIR /app
COPY /src /app/src
COPY ["package.json", "package-lock.json*", "./"]
# If you're using yarn:
# yarn build
RUN npm install --silent && mv node_modules ../
# Expose PORT 3000 on our virtual machine so we can run our server
EXPOSE 3000
version: '3.4'
services:
web:
build:
context: .
dockerfile: Dockerfile
environment:
- NODE_ENV=test
docker-compose -f docker-compose.yml -f test.yml build web
docker-compse run web [my_test_command]
Why use NextJS?By using NextJS, we can use both server-side rendering technique and using ReactJS library. An other plus to count is that it works smoothly with ExpressJS which is best NodeJS web framework right now. Last but not least, when using NextJS, I can easily configure default routes by seperating files, something similar to PHP. For example, when access to
/sample_route
, it would, by default executes content in sample_route.js
in server side. This is very convenient if we want to build a quick
demo application. Of course, still we can modify this behavior to make
routing more structural.website-scraper
package from npm. They provides a sample which uses AngularJS, so I want to create my own version using React.v9.3.0
5.6.0
npm
or yarn
with this boilerplate. Please follow their GitHub manual for installation instruction.sudo npm i -g next-express-bootstrap-boilerplate
cd
to source code folder, run command:next-boilerplate
npm run dev
yarn dev
/
root route, it would execute file /apps/page/index.js
, and for /profile
, it would execute file /apps/page/profile.js
to render UI. This is quite straightforward and easy to understand for beginner./profile
route, I delete file /apps/page/profile.js
and modify /apps/page/index.js
to add UI for URL input, submit form. Here is how new UI look like:Index
function doSubmitUrl
to do POST
call to /scrape_url
using fetch
. To understand fetch
, refer to how to Use Fetch.app.js
, add a function to handle POST /scrape_url
: app.post('/scrape_url', (req, res) => {
const scrapeUrl = req.body.url;
if (!scrapeUrl) {
return res.status(500).send('Please provide a valid URL');
}
const folderName = (new Date()).getTime();
const options = {
urls: [scrapeUrl],
directory: `tmp/${folderName}`,
}
scrape(options).then((result) => {
const zipFile = `./tmp/${folderName}.zip`;
zipFolder(options.directory, zipFile, (err) => {
if (err) {
console.log(err);
return res.status(500).send('zip file failed');
} else {
return res.download(zipFile);
}
})
}).catch((err) => {
console.log(err);
return res.status(500).send('zip file failed');
});
});
blob
response data, as a part of doSubmitUrl
function: doSubmitUrl() {
const data = { url: this.state.url }
const url = '/scrape_url';
fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: new Headers({
'Content-Type': 'application/json'
}),
})
.then((response) => response.blob())
.then((blob) => {
const link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.text=`${this.state.url}.zip`;
const downloadLink = document.getElementById('downloadLink');
downloadLink.appendChild(link);
})
.catch(error => console.error('Error:', error));
}