- 前端(用户界面)ssvoip.com
HTML + CSS + JavaScript (Web App)
对于Web App,你可以使用HTML来构建结构,CSS来添加样式,JavaScript来添加交互性。
html
Todo List
Add Todo<script src="script.js"></script>
/* styles.css */
body {
font-family: Arial, sans-serif;
}
/* script.js */
let todos = [];
function addTodo() {
const input = document.getElementById(‘todoInput’);
const text = input.value.trim();
if (text) {
todos.push(text);
updateTodoList();
input.value = ‘’;
}
}
function updateTodoList() {
const list = document.getElementById(‘todoList’);
list.innerHTML = ‘’; // Clear existing todos
todos.forEach((todo, index) => {
const item = document.createElement(‘li’);
item.textContent = todo;
// Optionally add a delete button or checkbox here
list.appendChild(item);
});
}
React Native (Mobile App)
对于移动App,React Native是一个流行的选择。
jsx
// App.js (React Native)
import React, { useState } from ‘react’;
import { View, TextInput, Button, FlatList, Text, StyleSheet } from ‘react-native’;
const TodoApp = () => {
const [todos, setTodos] = useState([]);
const [newTodo, setNewTodo] = useState(‘’);
const addTodo = () => {
if (newTodo.trim() !== ‘’) {
setTodos([…todos, newTodo]);
setNewTodo(‘’);
}
};
return (
<FlatList
data={todos}
renderItem={({ item }) => (
)}
keyExtractor={(item, index) => index.toString()}
/>
);
};
const styles = StyleSheet.create({
// Define styles here
});
export default TodoApp;
2. 后端(API服务器)
Node.js + Express (RESTful API)
javascript
// server.js
const express = require(‘express’);
const app = express();
const port = 3001;
let todos = []; // Simulated database
app.use(express.json());
app.get(‘/todos’, (req, res) => {
res.json(todos);
});
app.post(‘/todos’, (req, res) => {
const { text } = req.body;
if (!text) {
return res.status(400).send(‘Text is required’);
}
todos.push({ id: Date.now(), text, completed: false });
res.status(201).json(todos[todos.length - 1]);
});
// Start the server
app.listen(port, () => {
console.log(Server running on http://localhost:${port}
);
});
3. 数据库
对于数据库,你可以使用MongoDB、PostgreSQL、SQLite等。但在这里,我们已经在Node生成一个完整的App代码确实需要多种技术和语言,但在这里,我将提供一个非常简化的概述,展示如何使用不同的编程语言和技术栈来构建App的不同部分。请注意,由于篇幅和复杂性的限制,我将只提供核心概念的代码片段。
- 前端(用户界面)
HTML + CSS + JavaScript (Web App)
对于Web App,你可以使用HTML来构建结构,CSS来添加样式,JavaScript来添加交互性。
html
Todo List
Add Todo<script src="script.js"></script>
/* styles.css */
body {
font-family: Arial, sans-serif;
}
/* script.js */
let todos = [];
function addTodo() {
const input = document.getElementById(‘todoInput’);
const text = input.value.trim();
if (text) {
todos.push(text);
updateTodoList();
input.value = ‘’;
}
}
function updateTodoList() {
const list = document.getElementById(‘todoList’);
list.innerHTML = ‘’; // Clear existing todos
todos.forEach((todo, index) => {
const item = document.createElement(‘li’);
item.textContent = todo;
// Optionally add a delete button or checkbox here
list.appendChild(item);
});
}
React Native (Mobile App)
对于移动App,React Native是一个流行的选择。
jsx
// App.js (React Native)
import React, { useState } from ‘react’;
import { View, TextInput, Button, FlatList, Text, StyleSheet } from ‘react-native’;
const TodoApp = () => {
const [todos, setTodos] = useState([]);
const [newTodo, setNewTodo] = useState(‘’);
const addTodo = () => {
if (newTodo.trim() !== ‘’) {
setTodos([…todos, newTodo]);
setNewTodo(‘’);
}
};
return (
<FlatList
data={todos}
renderItem={({ item }) => (
)}
keyExtractor={(item, index) => index.toString()}
/>
);
};
const styles = StyleSheet.create({
// Define styles here
});
export default TodoApp;
2. 后端(API服务器)
Node.js + Express (RESTful API)
javascript
// server.js
const express = require(‘express’);
const app = express();
const port = 3001;
let todos = []; // Simulated database
app.use(express.json());
app.get(‘/todos’, (req, res) => {
res.json(todos);
});
app.post(‘/todos’, (req, res) => {
const { text } = req.body;
if (!text) {
return res.status(400).send(‘Text is required’);
}
todos.push({ id: Date.now(), text, completed: false });
res.status(201).json(todos[todos.length - 1]);
});
// Start the server
app.listen(port, () => {
console.log(Server running on http://localhost:${port}
);
});
3. 数据库
对于数据库,你可以使用MongoDB、PostgreSQL、SQLite等。但在这里,我们已经在Node