no message
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<!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>学生新增</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="" method="get">
|
||||
<table>
|
||||
<tr>
|
||||
<td>姓名:<input type="text" name="name"></td>
|
||||
<td>年龄:<input type="text" name="age"></td>
|
||||
<td>性别:<input type="text" name="sex"></td>
|
||||
<td>城市:<input type="text" name="city"></td>
|
||||
<td><button type="submit">提交</button></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,33 @@
|
||||
<!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>学生列表</title>
|
||||
<style>
|
||||
.table{
|
||||
width:520px;
|
||||
margin: 200px auto 0;
|
||||
}
|
||||
.table .hehe{
|
||||
font-weight: bold;
|
||||
font-size: 24px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="table">
|
||||
<table border="1" cellpadding="10">
|
||||
<tr class="hehe">
|
||||
<td width="100px">姓名</td>
|
||||
<td width="100px">年龄</td>
|
||||
<td width="100px">性别</td>
|
||||
<td width="100px">城市</td>
|
||||
<td width="300px">日期</td>
|
||||
</tr>
|
||||
@@
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Generated
+11
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"requires": true,
|
||||
"lockfileVersion": 1,
|
||||
"dependencies": {
|
||||
"moment": {
|
||||
"version": "2.29.1",
|
||||
"resolved": "https://registry.npm.taobao.org/moment/download/moment-2.29.1.tgz",
|
||||
"integrity": "sha1-sr52n6MZQL6e7qZGnAdeNQBvo9M="
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
http模块
|
||||
fs模块
|
||||
url模块
|
||||
server模块
|
||||
第三方模块
|
||||
res.setHeader('Location', '/'); //返回首页
|
||||
res.redirect('/');
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* @Author: chenxuan
|
||||
* @Date: 2021-02-06 23:10:16
|
||||
* @LastEditTime: 2021-10-21 00:37:35
|
||||
* @LastEditors: chenxuan
|
||||
*/
|
||||
//创建服务器
|
||||
var http = require('http');
|
||||
var fs = require('fs');
|
||||
var url = require('url');
|
||||
var moment = require('moment'); //引入第三方模块
|
||||
var server = http.createServer();
|
||||
var student=[
|
||||
{name:'陈轩',age:'18',sex:'男',city:'北京',time: moment().format('YYYY-MM-D h:mm:ss')},
|
||||
{name:'小白',age:'22',sex:'男',city:'成都',time: moment().format('YYYY-MM-D h:mm:ss')},
|
||||
{name:'小花',age:'20',sex:'男',city:'上海',time: moment().format('YYYY-MM-D h:mm:ss')},
|
||||
{name:'大白',age:'21',sex:'女',city:'武汉',time: moment().format('YYYY-MM-D h:mm:ss')},
|
||||
{name:'大黑',age:'21',sex:'女',city:'武汉',time: moment().format('YYYY-MM-D h:mm:ss')},
|
||||
{name:'大黑',age:'21',sex:'女',city:'武汉',time: moment().format('YYYY-MM-D h:mm:ss')}
|
||||
]
|
||||
server.on('request', function (req, res) {
|
||||
//console.log('正在监听用户请求');
|
||||
var urls = req.url;
|
||||
if (urls == '/') {
|
||||
//说明用户访问的是 localhost:8080
|
||||
fs.readFile('./index.html', 'utf-8', function (err, data) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
var html = '';
|
||||
student.forEach(function (item) {
|
||||
html += `
|
||||
<tr>
|
||||
<td>${item.name}</td>
|
||||
<td>${item.age}</td>
|
||||
<td>${item.sex}</td>
|
||||
<td>${item.city}</td>
|
||||
<td>${item.time}</td>
|
||||
</tr>`
|
||||
})
|
||||
var str = data.replace('@@', html);
|
||||
res.writeHead(200, { 'content-type': 'text/html;charset=utf-8'});
|
||||
res.end(str);
|
||||
})
|
||||
}
|
||||
else if (urls == '/add') {
|
||||
//添加页面
|
||||
fs.readFile('./add.html', 'utf-8', function (err, data) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, {'content-type': 'text/html;charset=utf-8'});
|
||||
res.end(data);
|
||||
})
|
||||
}
|
||||
else if (urls.indexOf('/add')==0) {
|
||||
if (req.method == 'POST') {
|
||||
//用户使用 post提交
|
||||
}
|
||||
else {
|
||||
//用户使用GET提交
|
||||
var params = url.parse(req.url, true).query;
|
||||
var newtime = moment().format('YYYY-MM-D h:mm:ss');
|
||||
var newstu = { name: params.name, age: params.age, sex: params.sex,city: params.city,time:newtime}
|
||||
student.push(newstu);
|
||||
// res.statusCode = 302;
|
||||
// res.setHeader('Location', '/'); //返回首页
|
||||
res.end();
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
server.listen(9999, function () {
|
||||
console.log('服务器已经启动9999')
|
||||
})
|
||||
@@ -0,0 +1,22 @@
|
||||
<!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>学生新增</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="" method="get">
|
||||
<table>
|
||||
<tr>
|
||||
<td>姓名:<input type="text" name="name"></td>
|
||||
<td>年龄:<input type="text" name="age"></td>
|
||||
<td>性别:<input type="text" name="sex"></td>
|
||||
<td>城市:<input type="text" name="city"></td>
|
||||
<td><button type="submit">提交</button></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,29 @@
|
||||
<!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>学生列表</title>
|
||||
<style>
|
||||
.table{
|
||||
width:520px;
|
||||
margin: 200px auto 0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="table">
|
||||
<table border="1" cellpadding="10">
|
||||
<tr>
|
||||
<td width="100px">姓名</td>
|
||||
<td width="100px">年龄</td>
|
||||
<td width="100px">性别</td>
|
||||
<td width="100px">城市</td>
|
||||
<td width="300px">日期</td>
|
||||
</tr>
|
||||
@@
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
//创建服务器
|
||||
var http = require('http');
|
||||
var fs = require('fs');
|
||||
var url = require('url');
|
||||
var server = http.createServer();
|
||||
|
||||
var luyou=require('./servers.js');
|
||||
luyou.servers1(server);
|
||||
|
||||
server.listen(8081, function () {
|
||||
console.log('服务器已经启动8080')
|
||||
});
|
||||
Generated
+11
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"requires": true,
|
||||
"lockfileVersion": 1,
|
||||
"dependencies": {
|
||||
"moment": {
|
||||
"version": "2.24.0",
|
||||
"resolved": "https://registry.npm.taobao.org/moment/download/moment-2.24.0.tgz",
|
||||
"integrity": "sha1-DQVdU/UFKqZTyfbraLtdEr9cK1s="
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
自定义模块
|
||||
@@ -0,0 +1,79 @@
|
||||
function servers(server){
|
||||
var fs = require('fs');
|
||||
var moment = require('moment'); //引入第三方模块
|
||||
var student=[
|
||||
{name:'陈轩',age:'18',sex:'男',city:'北京',time: moment().format('YYYY-MM-D h:mm:ss')},
|
||||
{name:'小白',age:'22',sex:'男',city:'成都',time: moment().format('YYYY-MM-D h:mm:ss')},
|
||||
{name:'小花',age:'20',sex:'男',city:'上海',time: moment().format('YYYY-MM-D h:mm:ss')},
|
||||
{name:'大白',age:'21',sex:'女',city:'武汉',time: moment().format('YYYY-MM-D h:mm:ss')},
|
||||
];
|
||||
server.on('request', function (req, res) {
|
||||
//console.log('正在监听用户请求');
|
||||
|
||||
var urls = req.url;
|
||||
|
||||
//console.log(urls)
|
||||
|
||||
if (urls == '/') {
|
||||
//说明用户访问的是 localhost:8080
|
||||
fs.readFile('./index.html', 'utf-8', function (err, data) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
|
||||
var html = '';
|
||||
student.forEach(function (item) {
|
||||
html += `
|
||||
<tr>
|
||||
<td>${item.name}</td>
|
||||
<td>${item.age}</td>
|
||||
<td>${item.sex}</td>
|
||||
<td>${item.city}</td>
|
||||
<td>${item.time}</td>
|
||||
</tr>`
|
||||
})
|
||||
|
||||
var str = data.replace('@@', html);
|
||||
|
||||
res.writeHead(200, { 'content-type': 'text/html;charset=utf-8' });
|
||||
res.end(str)
|
||||
})
|
||||
|
||||
}
|
||||
else if (urls == '/add') {
|
||||
//添加页面
|
||||
fs.readFile('./add.html', 'utf-8', function (err, data) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(200, { 'content-type': 'text/html;charset=utf-8' });
|
||||
res.end(data)
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
else if (urls.indexOf('/add')==0) {
|
||||
if (req.method == 'POST') {
|
||||
//用户使用 post提交
|
||||
|
||||
} else {
|
||||
//用户使用GET提交
|
||||
var params = url.parse(req.url, true).query;
|
||||
console.log(params);
|
||||
var newtime = moment().format('YYYY-MM-D h:mm:ss');
|
||||
var newstu = { name: params.name, age: params.age, sex: params.sex,city: params.city,time:newtime}
|
||||
student.push(newstu);
|
||||
res.statusCode = 302;
|
||||
res.setHeader('Location', '/')
|
||||
res.end();//注意
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
exports.servers1=servers;
|
||||
// export.变量名=方法名/属性名
|
||||
@@ -0,0 +1,3 @@
|
||||
document.getElementById('img').onclick=function(){
|
||||
alert("呵呵呵!")
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 608 KiB |
@@ -0,0 +1,24 @@
|
||||
<!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>处理静态资源</title>
|
||||
<style>
|
||||
.img{
|
||||
width: 1200px;
|
||||
/* height: 500px; */
|
||||
}
|
||||
.img img{
|
||||
width: 100%;
|
||||
/* height: 100%; */
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>处理静态资源</h1>
|
||||
<li class="img"><img src="img.jpg" alt="" id="img"></li>
|
||||
<script src="click.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,45 @@
|
||||
var http =require('http');
|
||||
var fs=require('fs');
|
||||
var server=http.createServer();
|
||||
server.on('request',function(req,res){
|
||||
// res.end('hello!');
|
||||
var urls=req.url;
|
||||
if(urls=='/'){
|
||||
fs.readFile('./index.html',function(err,data){
|
||||
if(err){
|
||||
console.log(err)
|
||||
}
|
||||
else{
|
||||
res.writeHead(200, { 'content-type': 'text/html;charset=utf-8' });
|
||||
res.end(data);
|
||||
}
|
||||
})
|
||||
}
|
||||
//挨个写入路由
|
||||
// else if(urls=='/img.jpg'){
|
||||
// fs.readFile('./img.jpg',function(err,data){
|
||||
// if(err){
|
||||
// console.log(err)
|
||||
// }
|
||||
// else{
|
||||
// res.writeHead(200, { 'content-type': 'text/html;charset=utf-8' });
|
||||
// res.end(data);
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
//通用方法
|
||||
else{
|
||||
fs.readFile('.'+req.url,function(err,data){
|
||||
if(err){
|
||||
console.log(err);
|
||||
}
|
||||
else{
|
||||
res.writeHead(200, { 'content-type': 'text/html;charset=utf-8' });
|
||||
res.end(data);
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
server.listen(8082,function(){
|
||||
console.log('8080端口启动成功!')
|
||||
})
|
||||
@@ -0,0 +1,8 @@
|
||||
处理静态资源
|
||||
js
|
||||
css
|
||||
img
|
||||
res.end() butter/String
|
||||
res.send() string/Array/Object/Buffer
|
||||
# 如果服务器端没有数据要返回到客户端的话,就直接用res.end()。
|
||||
# 如果服务器需要有数据返回到客户端的话,就需要用res.send().
|
||||
@@ -0,0 +1,20 @@
|
||||
const http=require('http')
|
||||
const server=http.createServer()
|
||||
server.on('request',function(req,res){
|
||||
const url=req.url;
|
||||
let content = '<h1>404 Not found!</h1>'
|
||||
if(url=='/' || url=='index'){
|
||||
content = '<h1>首页</h1>'
|
||||
}
|
||||
else if(url=='hehe'){
|
||||
content = '<h1>关于页面</h1>'
|
||||
}
|
||||
else{
|
||||
content = 'xxxxxxxxxxxxxxxx'
|
||||
}
|
||||
res.setHeader('Content-Type','text/html;charset=utf-8')
|
||||
res.end(content);
|
||||
})
|
||||
server.listen(9999, () =>{
|
||||
console.log('http server running at http://127.0.0.1:9999')
|
||||
})
|
||||
Reference in New Issue
Block a user