Professional Documents
Culture Documents
Contents
LABORATION’S GOAL ........................................................................................................................ 1
PRE-REQUIREMENTS ........................................................................................................................ 1
Instructions .......................................................................................................................................... 2
Creating a Toolchain from Scratch ...................................................................................................... 2
Initialization ....................................................................................................................................... 2
Webpack ............................................................................................................................................. 2
Setting Up React and Babel ............................................................................................................... 3
Setting up webpack-dev-server ........................................................................................................ 6
Prepare The Scripts in Package.Json ................................................................................................... 7
Adding The Server Part ........................................................................................................................ 7
Running it all ...................................................................................................................................... 7
TASKS ..................................................................................................................................................... 8
Extra Tasks: ........................................................................................................................................ 10
LABORATION’S GOAL
Build a table using react framework
PRE-REQUIREMENTS
In this second Lab you will need the following:
• NODE.JS
• Express
• A database server (MongoDB, PostgreSQL, or any other of your choice)
• React (which needs some prerequisites including package manager,
bundler and compiler)
Instructions
Instead of taking the whole huge “create-react-app” project, you are going to
install react with all the toolchains. It is expected that npm is already installed
in your machine as well as NodeJS (from lab1). The following will explain how
to set up React + Babel + Webpack.
Initialization
Create a new project where you will have React+Webpack+Babel. Create a
new directory with the name "yourname_react_app". Create package.JSON file
using npm init . (Please stick to the naming!)
Webpack
We need to install webpack as a dev dependency and webpack-cli so that you
can use webpack in the command line:
Create a src folder with index.js and place the following code as an
example:
console.log("hello");
Add the following scripts to your package.json
"scripts": {
"start": "webpack --mode development",
"build": "webpack --mode production"
This will create a dist folder with main.js file inside (containing your src
code).
Now, create a webpack.config.js file to state the rules for our babel-
loader.
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
};
We then need to make a separate file called .babelrc to provide the options
for babel-loader. You can include it in the webpack.config.js file, or have a
separate file which results in clearer readability.
{
"presets": ["env", "react"]
}
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
ReactDOM.render(element, document.getElementById("index"));
Create an index.html file in the src folder where you can add your
section element with id “index”.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Lab 2 react</title>
</head>
<body>
<section id="index"></section>
</body>
</html>
This plugin generates an HTML file with <script> injected, writes this to
dist/index.html, and minifies the file.
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
},
plugins: [htmlPlugin]
};
plugins: [
new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
});
]
If you now run npm run start you should see index.html being generated
in the dist folder.
Run open dist/index.html and you should see “Hello World!” in your
browser.
Setting up webpack-dev-server
It is a bit tedious to keep running this command every time you want to see
your changes in the browser. To have webpack “watch” our changes and thus
refresh whenever we have made changes to any of our components, we can
use webpack-dev-server module.
"scripts": {
If you now run npm run start you should see localhost:8080 open up in your
default browser — that’s what the —-open flag is for. Now every time you
make changes, it will refresh the page.
Now you have a React + Webpack + Babel project, so you can create and
develop the React-code with JSX syntax easily. At this point, you shall be able
to play around with React code and see it update in real-time in your browser
with the Webpack dev- server.
Note: you can't run both "npm start" and "node server.js" in the same shell! In
that case you have to install "concurrently" which is an npm package
Running it all
1. Start the server in one terminal window "node server.js", and let’s
assume it listens at 3000.
2. Then "npm start" in another terminal window. This will automatically
launch the Webpack dev server at localhost:8080 and the index.html
page will render in your browser. This is the Webpack dev-server
showing your HTML-file with your React-code included.
But we want to use the Express-server so that we have full control over
the back-end as well!
3. Navigate to localhost:3000 instead. That's where your Express server
is running and it should now also show the same index.html file!
You are now connected with your own Express server!
4. Now that you have the connection up and the Express server is (should
be) connected to database, let your React code do some client-side AJAX
call to fetch data from a DB table and show it on the home page!
Note
If you do changes "on the fly" in the React code on the Webpack dev-server, it
won't show in the Express server! It's because the Webpack dev-server only
autogenerates the JS-code in its own local copy of the dist-folder (wherever
that is...).
So, if you look inside your own project's dist folder, you will see that the index
file and JS-code hasn't updated. And this is the code that your Express server
delivers. To have the Express server reflect changes, you must manually
rebuild with Webpack: "npm run build". That will update your dist-folder, and
you can also verify that.
TASKS
You shall create a very simple back-end server that has one database which
consists of three tables. The database should be on the cloud (not local)
Student
This table shall have the following attributes:
• unit_id, max 50 characters (should be unique)
• full name
• email
• hashed_password (optional if you add users manually)
Registration
• student_id
• course_code
• unix_timestamp the time the student registered to a new course
Course
This table shall have the following attributes:
• course_code, max 50 characters (should be unique)
• course name
• course description
Add at least 5 rows in each table with some initial values. In the table
registration, there shall be different values on timestamps. Make sure that if
the data in the database table is changed, the reflected changes shall show on
the webpage! You need only to show one table explained below.
The UI front-end part shall be done with React, meaning as much as possible in
the UI shall be generated client-side from JavaScript with the React library.
Only fetch the minimum amount of data from the server and then generate as
much as possible of the UI from JavaScript code with React components.
The homepage (e.g. index.html) shall display a table of the latest 5 registered
students. The table headers shall be “clickable” so that the table should be re-
ordered according to the clicked header.
Student_ID Student name Course name Registration time
Please submit .zip file with all the required files (except node_modules) no
later than 14th April
Best of Wishes!