Building a web and mobile app in 5 minutes with NodeJS, Express, and React Native
To start building a web and mobile app, we first need to set up the backend of the app using NodeJS and Express. Express is a minimal web framework for NodeJS that allows developers to easily create web applications and APIs. In this case, we will create a simple RESTful API for our newspaper app.
To begin, we will use the Node Package Manager (NPM) to create a new Express project. Open a terminal and navigate to the directory where you want to create your project.
Run the following command:
npm init
This command will create a package.json file in your project directory, which contains information about your project and its dependencies. Next, we will install Express by running the following command:
npm install express --save
Now, let’s create a new file called server.js in the root of your project directory and add the following code:
const express = require('express');
const app = express();
const port = 3000;
// create an array to store articles
let articles = [
{ id: 1, title: 'Article 1', content: 'Content 1' },
{ id: 2, title: 'Article 2', content: 'Content 2' },
{ id: 3, title: 'Article 3', content: 'Content 3' }
];
// create an endpoint to get all articles
app.get('/articles', (req, res) => {
res.json(articles);
});
// create an endpoint to get an article by id
app.get('/articles/:id', (req, res) => {
const article = articles.find(a => a.id === parseInt(req.params.id));
if (!article) return res.status(404).send('Article not found');
res.json(article);
});
// create an endpoint to create an article
app.post('/articles', (req, res) => {
const article = {
id: articles.length + 1,
title: req.body.title,
content: req.body.content
};
articles.push(article);
res.json(article);
});
// create an endpoint to update an article
app.put('/articles/:id', (req, res) => {
const article = articles.find(a => a.id === parseInt(req.params.id));
if (!article) return res.status(404).send('Article not found');
article.title = req.body.title;
article.content = req.body.content;
res.json(article);
});
// create an endpoint to delete an article
app.delete('/articles/:id', (req, res) => {
const article = articles.find(a => a.id === parseInt(req.params.id));
if (!article) return res.status(404).send('Article not found');
const index = articles.indexOf(article);
articles.splice(index, 1);
res.json(article);
});
app.listen(port, () => console.log(`Newspaper API listening at http://localhost:${port}`));
Now that we have set up the backend of our app, we can move on to setting up the front end of our app using React Native. React Native allows developers to build mobile apps using React, the same JavaScript library that we use for building web apps.
To get started, we need to install the React Native CLI by running the following command:
npm install -g react-native-cli
Then, we can create a new React Native project by running the following command:
react-native init NewspaperApp
This command will create a new directory called “NewspaperApp” in your project directory, which contains a basic React Native app.
Next, let’s create a new file called ArticleList.js in the root of your React Native project directory and add the following code:
import React, { useState, useEffect } from 'react';
import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native';
const ArticleList = () => {
const [articles, setArticles] = useState([]);
useEffect(() => {
fetch('http://localhost:3000/articles')
.then(response => response.json())
.then(data => setArticles(data))
.catch(error => console.error(error));
}, []);
return (
<View style={styles.container}>
<FlatList
data={articles}
renderItem={({ item }) => (
<TouchableOpacity style={styles.item} onPress={() => console.log(item)}>
<Text style={styles.title}>{item.title}</Text>
<Text style={styles.content}>{item.content}</Text>
</TouchableOpacity>
)}
keyExtractor={item => item.id.toString()}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: '
center', }, item: { padding: 16, borderBottomWidth: 1, borderBottomColor: '#ccc', }, title: { fontWeight: 'bold', fontSize: 18, marginBottom: 8, }, content: { fontSize: 14, }, });
export default ArticleList;
This code creates a simple React Native component that displays a list of articles. The component uses the useEffect hook to fetch the list of articles from the backend API when the component is first rendered. The fetched articles are then displayed in a FlatList.
Finally, to run the app on your device or emulator, you can use the following command:
react-native run-ios ||react-native run-android
In conclusion, using NodeJS, Express, and React Native, it is possible to quickly and easily build web and mobile apps. The combination of these technologies allows developers to create robust and scalable apps with minimal effort. The code provided in this article is just a simple example of what can be achieved with these technologies, but it can be easily extended to include more features and functionality. With this simple guide, you can now start experimenting with building your own web and mobile apps.