如何在nodejs中加载内容类型.ttf和.woff?

时间:2018-02-24 11:44:13

标签: html node.js load true-type-fonts woff

当我在nodejs服务器中加载我的html模板时,我收到这些错误,除了图标之外正确加载了孔模板:

index.html:189 GET http://127.0.0.1:5050/assets/web/assets/mobirise-icons/mobirise-icons.ttf?spat4u net :: ERR_ABORTED

index.html:1 GET http://127.0.0.1:5050/assets/web/assets/mobirise-icons/mobirise-icons.woff?spat4u net :: ERR_ABORTED

这是我的server.js:



var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
var mysql = require('mysql');

http.createServer(function (request, response) {
    console.log('attente de requètes...');
	var q = url.parse(request.url, true).query;
	var filePath = '.' + request.url;
	console.log(request.url);
	if (filePath == './')
		filePath = './index.html';
	console.log(filePath);	
	var extname = path.extname(filePath);
	var contentType = 'text/html';
	switch (extname) {
		case '.js':
			contentType = 'text/javascript';
			break;
		case '.css':
			contentType = 'text/css';
			break;
	}

	
	fs.exists(filePath, function(exists) {
	

	if (exists) {
			fs.readFile(filePath, function(error, content) {
				if (error) {
					response.writeHead(500);
					response.end();
				}
				else {
					response.writeHead(200, { 'Content-Type': contentType });
					response.end(content, 'utf-8');
				}
			});
		}
		else {
			if(q.email!=null){
				console.log(q.email+" "+q.pswd);
				var con = mysql.createConnection({
				  host: "127.0.0.1",
				  user: "root",
				  password: "",
				  database: "Cyberwind"
				});
	
				con.connect(function(err) {
					if (err) throw err;
					con.query("INSERT INTO inscription (email, password, confirmpassword) VALUES ('"+q.email+"','"+q.pswd+"','"+q.confirmpassword+"')", function (err, result, fields) {
						if (err) throw err;
						response.end();
					});
				}); 
			}
			else {
				response.writeHead(404);
				console.log("iciici")
				response.end();
			}
		}
	});
	
}).listen(5050);
console.log('Server running at 5050');




我想知道如何在' switch(extname)'中添加2个案例contentType为(.ttf)或(.wof)。

0 个答案:

没有答案