- 前端(React Native, JavaScript/TypeScript)
App.js (React Native)yinanjinying.com
javascript
import React, { useState } from ‘react’;
import { View, Text, TextInput, Button, FlatList, StyleSheet } from ‘react-native’;
const TodoApp = () => {
const [todos, setTodos] = useState([]);
const [newTodo, setNewTodo] = useState(‘’);
const addTodo = () => {
if (newTodo.trim()) {
setTodos([…todos, { id: todos.length + 1, text: newTodo }]);
setNewTodo(‘’);
}
};
return (
<TextInput
style={styles.input}
value={newTodo}
onChangeText={text => setNewTodo(text)}
placeholder=“Add a todo”
/>
<FlatList
data={todos}
renderItem={({ item }) => (
)}
keyExtractor={item => item.id.toString()}
/>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
justifyContent: ‘center’,
},
input: {
height: 40,
borderColor: ‘gray’,
borderWidth: 1,
padding: 10,
marginBottom: 10,
},
todoItem: {
padding: 10,
backgroundColor: ‘#f9c2ff’,
marginBottom: 5,
},
});
export default TodoApp;
2. 后端(Node.js + Express, JavaScript)
server.js (Node.js + Express)
javascript
const express = require(‘express’);
const app = express();
const port = 3000;
let todos = []; // 假设的待办事项列表
// 中间件用于解析JSON格式的请求体
app.use(express.json());
// 获取待办事项列表
app.get(‘/todos’, (req, res) => {
res.json(todos);
});
// 添加待办事项
app.post(‘/todos’, (req, res) => {
const newTodo = { id: todos.length + 1, text: req.body.text };
todos.push(newTodo);
res.status(201).json(newTodo);
});
app.listen(port, () => {
console.log(Server running on port ${port}
);
});
3. 数据库(MongoDB + Mongoose, Node.js)
虽然上面的Node.js服务器示例中我们使用了内存中的数组来存储待办事项,但通常你会想将它们存储在数据库中。以下是使用MongoDB和Mongoose的示例。
首先,安装Mongoose:npm install mongoose
mongooseModel.js
javascript
const mongoose = require(‘mongoose’);
const TodoSchema = new mongoose.Schema({
text: String,
});
const Todo = mongoose.model(‘Todo’, TodoSchema);
module.exports = Todo;
serverWithMongoose.js
javascript
const express = require(‘express’);
const mongoose = require(‘mongoose’);
const Todo = require(‘./mongooseModel’);
const app = express();
const port = 3000;
mongoose.connect(‘mongodb://localhost:27017/mydatabase’, {
useNewUrlParser: true,
useUnifiedTopology: true
});
// 路由…(与上面的Express示例相似,但使用Todo模型进行数据库操作)
app.listen(port, () => {
console.log(Server with Mongoose running on port ${port}
);
});
注意:上面的serverWithMongoose.js示例中省略了实际的路由处理,因为这与server.js中的示例类似,只是你现在会使用Todo模型来与MongoDB数据库交互。生成一个完整的App代码确实需要多种技术和语言,但在这里,我将为你概述一个简单App(如待办事项列表)在不同层面的关键组件和示例代码片段,而不是一个完整的、可直接编译的项目。
- 前端(React Native, JavaScript/TypeScript)
App.js (React Native)
javascript
import React, { useState } from ‘react’;
import { View, Text, TextInput, Button, FlatList, StyleSheet } from ‘react-native’;
const TodoApp = () => {
const [todos, setTodos] = useState([]);
const [newTodo, setNewTodo] = useState(‘’);
const addTodo = () => {
if (newTodo.trim()) {
setTodos([…todos, { id: todos.length + 1, text: newTodo }]);
setNewTodo(‘’);
}
};
return (
<TextInput
style={styles.input}
value={newTodo}
onChangeText={text => setNewTodo(text)}
placeholder=“Add a todo”
/>
<FlatList
data={todos}
renderItem={({ item }) => (
)}
keyExtractor={item => item.id.toString()}
/>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
justifyContent: ‘center’,
},
input: {
height: 40,
borderColor: ‘gray’,
borderWidth: 1,
padding: 10,
marginBottom: 10,
},
todoItem: {
padding: 10,
backgroundColor: ‘#f9c2ff’,
marginBottom: 5,
},
});
export default TodoApp;
2. 后端(Node.js + Express, JavaScript)
server.js (Node.js + Express)
javascript
const express = require(‘express’);
const app = express();
const port = 3000;
let todos = []; // 假设的待办事项列表
// 中间件用于解析JSON格式的请求体
app.use(express.json());
// 获取待办事项列表
app.get(‘/todos’, (req, res) => {
res.json(todos);
});
// 添加待办事项
app.post(‘/todos’, (req, res) => {
const newTodo = { id: todos.length + 1, text: req.body.text };
todos.push(newTodo);
res.status(201).json(newTodo);
});
app.listen(port, () => {
console.log(Server running on port ${port}
);
});
3. 数据库(MongoDB + Mongoose, Node.js)
虽然上面的Node.js服务器示例中我们使用了内存中的数组来存储待办事项,但通常你会想将它们存储在数据库中。以下是使用MongoDB和Mongoose的示例。
首先,安装Mongoose:npm install mongoose
mongooseModel.js
javascript
const mongoose = require(‘mongoose’);
const TodoSchema = new mongoose.Schema({
text: String,
});
const Todo = mongoose.model(‘Todo’, TodoSchema);
module.exports = Todo;
serverWithMongoose.js
javascript
const express = require(‘express’);
const mongoose = require(‘mongoose’);
const Todo = require(‘./mongooseModel’);
const app = express();
const port = 3000;
mongoose.connect(‘mongodb://localhost:27017/mydatabase’, {
useNewUrlParser: true,
useUnifiedTopology: true
});
// 路由…(与上面的Express示例相似,但使用Todo模型进行数据库操作)
app.listen(port, () => {
console.log(Server with Mongoose running on port ${port}
);
});
注意:上面的serverWithMongoose.js示例中省略了实际的路由处理,因为这与server.js中的示例类似,只是你现在会使用Todo模型来与MongoDB数据库交互。