我无法在本地主机上启动简单的Node.js Express.js服务器。
我尝试过
节点Express.js
无济于事。
我收到此错误
$ node Express.js
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EACCES 127.0.0.1
at Object._errnoException (util.js:992:11)
我的代码是
// express.js
const mysql = require('mysql');
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const hostname = '127.0.0.1';
const port = '8080';
const app = express();
app.set('port', process.env.PORT || 8080);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const con = mysql.createConnection({
host: 'localhost',
user: 'bojan',
password: 'Hamurabi77',
database: 'user'
});
con.connect((err) => {
if (err) throw err;
console.log('Connected!');
});
const createDatabase = 'CREATE DATABASE user';
con.query(createDatabase, (err, result, fields) => {
if (err) throw err;
console.log('Created!');
});
const createTable = 'CREATE TABLE user (id AUTO_INCREMENT PRIMARY_KEY something VARCHAR(255))';
con.query(createTable, (err, result, fields) => {
if (err) throw err;
console.log('Created!');
});
app.get('/users', (req, res, end) => {
const selectUsers = `SELECT * FROM user`;
con.query(selectUsers, (err, result, fields) => {
if (err) throw err;
console.log('Selected!');
});
res.json();
res.send('Selected!');
res.end();
});
app.get('/users/:id', (req, res, end) => {
const selectUserByID = `SELECT * FROM user WHERE id=${req.id}`;
con.query(selectUserByID, (err, result, fields) => {
if (err) throw err;
console.log('Selected by ID!');
});
res.json();
res.send('Selected by ID!');
res.end();
});
app.post('/user/insert', (req, res, end) => {
const insertUser = `INSERT INTO user SET (something) VALUES (${req.something})`;
con.query(insertUser, (err, result, fields) => {
if (err) throw err;
console.log('Inserted!');
});
res.json();
res.send('Inserted!');
res.end();
});
app.post('/user/update/:id', (req, res, end) => {
const updateUserByID = `UPDATE user SET (something) VALUES (${req.something}) WHERE id=${req.id}`;
con.query(updateUserByID, (err, result, fields) => {
if (err) throw err;
console.log('Updated!');
});
res.json();
res.send('Updated!');
res.end();
});
app.post('/user/delete/:id', (req, res, end) => {
const deleteUserByID = `DELETE * FROM user WHERE id=${req.id}`;
con.query(deleteUserByID, (err, result, fields) => {
if (err) throw err;
console.log('Deleted!');
});
res.json();
res.send('Deleted!');
res.end();
});
router(app);
app.listen(hostname, port, () => {
console.log(`Connection running at https://${hostname}:${port}!`);
});
我的代码有什么问题?任何建议将不胜感激。 我希望它可以在https://localhost:8080
上使用答案 0 :(得分:0)
可能是您在本地计算机上创建服务器时遇到了问题。
const http = require('http');
router(app);
const server = http.createServer(app);
server.listen(3090, () => {
console.log('Server is on port 3090');
});
您可以尝试一下吗? 如果有任何错误,请对其进行评论。
答案 1 :(得分:0)
无需在此处提供主机名,请尝试以下代码:
public class CustomTitleBarWindow: Window
{
public static readonly DependencyProperty ShowSystemMenuCommandProperty = DependencyProperty.Register("ShowSystemMenuCommand", typeof(ICommand), typeof(CustomTitleBarWindow), new PropertyMetadata(null));
public ICommand ShowSystemMenuCommand
{
get => (ICommand)GetValue(ShowSystemMenuCommandProperty);
set => SetValue(ShowSystemMenuCommandProperty, value);
}
public static readonly DependencyProperty MinimizeCommandProperty = DependencyProperty.Register("MinimizeCommand", typeof(ICommand), typeof(CustomTitleBarWindow), new PropertyMetadata(null));
public ICommand MinimizeCommand
{
get => (ICommand)GetValue(MinimizeCommandProperty);
set => SetValue(MinimizeCommandProperty, value);
}
public static readonly DependencyProperty MaximizeCommandProperty = DependencyProperty.Register("MaximizeCommand", typeof(ICommand), typeof(CustomTitleBarWindow), new PropertyMetadata(null));
public ICommand MaximizeCommand
{
get => (ICommand)GetValue(MaximizeCommandProperty);
set => SetValue(MaximizeCommandProperty, value);
}
public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.Register("CloseCommand", typeof(ICommand), typeof(CustomTitleBarWindow), new PropertyMetadata(null));
public ICommand CloseCommand
{
get => (ICommand)GetValue(CloseCommandProperty);
set => SetValue(CloseCommandProperty, value);
}
}
答案 2 :(得分:0)
尝试一下,先输入端口名,然后输入主机名
app.listen(port, hostname, () => {
console.log(`Connection running at https://${hostname}:${port}!`);
});